Problem
Same Tree
Easy- trees
Decide whether the binary trees p and q are the same tree. They are when they have the same shape and every matching position holds an equal value. Return true if so and false if not.
Trees in this problem are written as level-order arrays: the root first, then each level from left to right. null marks a missing child, and a missing child has no entries beneath it. Trailing nulls are omitted.
- Input
p = [5, 2, 8, 1, 3], q = [5, 2, 8, 1, 3]- Output
true- Explanation
both trees have the same shape and hold the same value in every position.
- Input
p = [4, 2, 7], q = [4, 2, 9]- Output
false- Explanation
the shapes match, but the right child is 7 in
pand 9 inq.
- Input
p = [6, 3, null, 1], q = [6, 3, null, null, 1]- Output
false- Explanation
both trees hold 6, 3 and 1, but the 1 is the left child of 3 in
pand the right child of 3 inq.
Constraints:
pandqmay each be empty, and two empty trees are the same
Tab indents. Press Esc, then Tab to leave the editor.
Run your code to see every test here. Nothing is submitted or recorded.