root of a binary tree whose values are all distinct, and an integer start, the value of one of its nodes.
A leaf is a node with no children. The distance between two nodes is the number of edges on the path between them, and a path may go down to children as well as up to parents.
Find the leaf closest to the node with value start and return its value. If start is a leaf itself, the answer is start.
The tests are built so that the closest leaf is unique.root=tree_node([10, 20, 30, None, None, 40, 50, 60]), start=40 → 60
Below node 40 is the leaf 60, one edge away. Leaf 50 takes two edges through the parent 30, and leaf 20 is even farther.
root=tree_node([3, 8, None, 6, None, 1]), start=8 → 1
The root 3 has a child, so it is not a leaf. The only leaf is 1, two edges down.
root=tree_node([5]), start=5 → 5
A single-node tree: the node itself is a leaf at distance 0.
Node.val ≤ 1000start existsroot=tree_node([10, 20, 30, None, None, 40, 50, 60]), start=40
60
Run checks the first 3 tests in your browser. Submit checks all 74 tests on the server, hidden ones included.