root of a binary tree whose node values are all distinct, and two values first and second, both present in the tree.
Return the distance between the nodes holding these values: the number of edges on the path from one to the other. Edges can be walked in both directions, up and down. If first equals second, the distance is 0.root=tree_node([10, 4, 15, 2, 7, None, 20]), first=7, second=20 → 4
The path 7 → 4 → 10 → 15 → 20 uses four edges.
root=tree_node([10, 4, 15, 2, 7, None, 20]), first=4, second=2 → 1
2 is a child of 4, so they are one edge apart.
root=tree_node([8, 3]), first=3, second=3 → 0
A node is 0 edges away from itself.
Node.val ≤ 10⁹, all values are distinctfirst and second are present in the treeroot=tree_node([10, 4, 15, 2, 7, None, 20]), first=7, second=20
4
Run checks the first 3 tests in your browser. Submit checks all 33 tests on the server, hidden ones included.