Skip to content
CodeShare
All problems

Same Tree

Check whether two binary trees are identical.

EasyTrees

Given the roots of two binary trees p and q, return true if they are the same tree: the same shape, with the same value in every node.

Trees are written level by level, with null for a missing child.

Examples

Example 1
Input
p = [1, 2, 3], q = [1, 2, 3]
Output
true
Example 2
Input
p = [1, 2], q = [1, null, 2]
Output
false
Why
The 2 is a left child in one tree and a right child in the other.
Example 3
Input
p = [1, 2, 1], q = [1, 1, 2]
Output
false

Constraints

  • Each tree has between 0 and 100 nodes.
  • -10^4 <= Node.val <= 10^4