351. Are Two Trees Identical
Two trees are given. Print Yes if they have the same shape AND the same
values in the same positions, and No otherwise.
Shape counts. These two hold identical values and are not identical trees, because one leans right and the other leans left:
1 1
\ /
2 2
\ \
3 3
Walk both together. At each step: both absent is a match, one absent is a failure, different values is a failure, and otherwise compare the two left children and the two right children.
Constraints - `1 ≤ n ≤ 100000` - `1 ≤ m ≤ 100000` - `-1000000000 ≤ value ≤ 1000000000` - Both inputs always form valid trees rooted at node 1.
Input
The first line contains two integers n and m, the sizes of the two trees.
The next 1 + n lines describe the first tree: its n values, then n lines
of left and right children.
The next 1 + m lines describe the second tree in the same way.
Node 1 is the root of each tree, and 0 means no child.
Output
Print Yes or No on one line.
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.