Skip to content
Onsite

Same Tree

EasyP2Coding

Given the roots of two binary trees p and q, return true if they have the same shape and every pair of corresponding nodes holds the same value, otherwise false.

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 values match but 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

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

Hints

Run checks the 3 visible tests, Submit all 9.