Loading problem...
Given the root of a binary tree, determine the length of the shortest path from the root node to any leaf node in the tree.
The path length is measured as the count of nodes encountered along the route from the root down to the closest leaf. A leaf node is defined as a node that has no child nodes (both left and right children are null).
Your task is to traverse the tree and identify the leaf that can be reached with the minimum number of node hops, then return that count.
Important Considerations:
root = [3,9,20,null,null,15,7]2The tree structure is: 3 / 9 20 / 15 7
The shortest path from root to a leaf goes from node 3 → node 9. Node 9 is a leaf (no children), and this path has length 2. The alternative paths (3 → 20 → 15 or 3 → 20 → 7) have length 3.
root = [2,null,3,null,4,null,5,null,6]5This tree is essentially a right-skewed chain: 2
3
4
5
6
Since each node only has a right child until node 6, which is the only leaf, the shortest (and only) path has length 5: 2 → 3 → 4 → 5 → 6.
root = [1,2,3]2The tree has root 1 with children 2 and 3. Both children are leaves. Any path from root to a leaf (either 1 → 2 or 1 → 3) has length 2.
Constraints