Same Tree
Given two binary trees (level-order arrays), check if they are structurally identical with the same node values.
Example
1,2,3 1,2,3
True
- Each input line is one binary tree in level order: the root first, then its left and right children, then the next level left to right. Both lines here are
1,2,3. - Drawn out, each tree has root with left child and right child ; both children are leaves.
- Two trees are the same when every position agrees: a node exists at a position in one tree exactly when it exists there in the other, and the values there are equal.
- Root: . Left of the root: . Right of the root: . Below the leaves both trees are empty, so no position is filled in one but missing in the other.
- Every position agrees in shape and value, so the answer is
True. - Notice that equal values are not enough: root with a left child and root with a right child hold the same values but differ, because an empty position is part of the shape.
Constraints
- 0 <= number of nodes <= 100