Skip to content

Same Tree

EasyTrees
Decode it first. Clues, constraints, pattern and plan before any code.

Given the roots of two binary trees p and q, return True if they are identical: same shape, and the same value in every matching position. Otherwise return False.

Trees in the examples are written in level order (top to bottom, left to right), with null for a missing child, the same way the tests build them.

p = [1, 2, 3],  q = [1, 2, 3]     →  True
p = [1, 2],     q = [1, null, 2]  →  False   (2 is on different sides)
p = [1, 2, 1],  q = [1, 1, 2]     →  False

Constraints

  • 0 <= number of nodes in each tree <= 100
  • -10^4 <= Node.val <= 10^4