Loading...
Loading...
Loading...
Print the number of links on the route between two nodes of the tree. A route may go up and then down. All values are different and both nodes are in the tree. If both values are the same, the distance is 0.
Example 1
Input
3 5 1 6 2 0 8 null null 7 4 5 1
Output
2
5 to 3 to 1 is 2 links.
Example 2
Input
3 5 1 6 2 0 8 null null 7 4 7 4
Output
2
7 to 2 to 4 is 2 links.
Try the problem first. Open one hint at a time.
Find the lowest common ancestor first.
The distance is the distance from it to p plus the distance from it to q.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor