Closest Leaf in a Binary Tree - Problem

Imagine you're exploring a binary tree and need to find the shortest path from a specific target node to any leaf node. Given the root of a binary tree where every node has a unique value and a target integer k, your task is to return the value of the nearest leaf node to the target k.

The distance is measured by the minimum number of edges you need to traverse to reach any leaf from the target node. Remember, a leaf node is one that has no children (both left and right are null).

Key Points:

  • You can move both up and down the tree from the target node
  • Distance is measured in number of edges, not node values
  • A leaf has no left or right children
  • All node values are unique

Input & Output

example_1.py — Basic Tree
$ Input: root = [1,3,2], k = 1
Output: 2
💡 Note: The tree has structure: 1(root) with children 3(left) and 2(right). Both 3 and 2 are leaves. From node 1 (our target k), we can reach leaf 3 in 1 step and leaf 2 in 1 step. Since both have the same distance, we return either one. The answer returns 2.
example_2.py — Target is Leaf
$ Input: root = [1,2,3,4,null,null,null,5,null,6], k = 2
Output: 3
💡 Note: Tree structure: 1→(2,3), 2→(4,null), 4→(5,null), 5→(6,null). Target k=2 is not a leaf (has child 4). Leaves are: 3, 6. Distance from 2 to 3: go up to 1, then down to 3 = 2 steps. Distance from 2 to 6: down through 4→5→6 = 3 steps. Closest leaf is 3.
example_3.py — Target is Leaf Node
$ Input: root = [1,2,3], k = 3
Output: 3
💡 Note: The tree is 1→(2,3). Target k=3 is already a leaf node (no children). When the target itself is a leaf, the distance is 0, so we return the target value 3.

Constraints

  • The number of nodes in the tree is in the range [1, 103]
  • 1 ≤ Node.val ≤ 103
  • All Node.val are unique
  • k is guaranteed to exist in the tree

Visualization

Tap to expand
Closest Leaf in a Binary Tree INPUT Binary Tree Structure: 1 (TARGET k=1) 3 LEAF 2 LEAF Input Values: root = [1, 3, 2] k = 1 Find closest leaf to node 1 ALGORITHM (BFS) 1 Build Parent Map Map each node to parent 2 Find Target Node Locate node with value k 3 BFS from Target Explore: left, right, parent 4 Return First Leaf First leaf found = closest BFS Traversal: Queue: [1] Visit 1 --> add 3, 2 Queue: [3, 2] Visit 3 (leaf!) dist=1 Visit 2 (leaf!) dist=1 FINAL RESULT Shortest Path to Leaf: 1 2 3 Distance = 1 edge Output: 2 Key Insight: BFS guarantees the first leaf found is the closest because it explores nodes level by level. Building a parent map allows traversal in all directions: left child, right child, AND parent node. Both leaves (2 and 3) are at distance 1, so either answer is valid. Output: 2 TutorialsPoint - Closest Leaf in a Binary Tree | BFS Approach
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 22
47.3K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen