Objective
The lowest common ancestor of two nodes is the deepest node that has both as descendants (a node counts as a descendant of itself). If you store the path from the root to each node, it is the last common element of both paths. The first line has the number of nodes n; then come n lines value left right, where left and right are the indexes (from 0) of the children or -1 if there is no child. Node 0 is the root. All values are different.
Instructions
- After the nodes comes a line with two values
a bthat exist in the tree. - Show
Common ancestor: v.
7
3 1 2
5 3 4
1 5 6
6 -1 -1
2 -1 -1
0 -1 -1
8 -1 -1
6 2
Common ancestor: 5
Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.
Start exercise