Check Whether Two Trees Match Exactly

Implement treesMatch

You are given the roots of two binary trees. Decide whether they are exactly the same: they must have the same shape, and each pair of corresponding nodes must hold the same value. Two empty trees are considered the same.

Comparing the trees node by node is the direct approach. A tempting shortcut — comparing only the list of values — is wrong, because the same values can be arranged in different shapes.

Example 1:

Input: a = [7,3,9,1], b = [7,3,9,null,1]

Output: false

Example 2:

Input: a = [5,2,8], b = [5,2,8]

Output: true

Example 3:

Input: a = [], b = []

Output: true

+ 13 hidden test cases run on Submit.

Constraints:

  • ●0 ≤ number of nodes in each tree ≤ 100
  • ●−1000 ≤ node.val ≤ 1000
  • ●Each tree is given by its root node (null for an empty tree); each node has a val, a left child and a right child
  • ●Two trees match when they have the same shape AND every pair of corresponding nodes holds the same value. Return true if they match, false otherwise

a =

[7, 3, 9, 1]

b =

[7, 3, 9, null, 1]