Problem 147
Same Tree
Easy · 10 pts · Tree, Depth-First Search, Binary Tree
Problem
You are given two binary trees. Decide whether they are identical: they must have exactly the same shape, and every pair of nodes in the same position must hold the same value. Compare them recursively. Two empty trees are the same, an empty and a non-empty tree are not, and two non-empty trees are the same if their roots have equal values and both their left subtrees and their right subtrees are the same.
Input
The first tree, then the second tree, each as two lines: the token count N and the N level-order tokens ('null' for missing children).
Output
Print YES or NO.
Constraints
1 <= number of nodes in each tree <= 100; -10000 <= value <= 10000
Example input
3 1 2 3 3 1 2 3
Example output
YES
Explanation
Both trees have root 1 with left child 2 and right child 3. The shapes and all values match, so the answer is YES.