Minimum Depth of Binary Tree - Problem

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

Note: A leaf is a node with no children.

Input & Output

Example 1 — Basic Tree
$ Input: root = [3,9,20,null,null,15,7]
Output: 2
💡 Note: The shortest path is from root 3 to leaf 9, which has depth 2 (3 → 9). The other leaves are at depth 3.
Example 2 — Single Node
$ Input: root = [2]
Output: 1
💡 Note: The root is also a leaf node, so the minimum depth is 1.
Example 3 — Skewed Tree
$ Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
💡 Note: This is a right-skewed tree where the only leaf is at depth 5: 2 → 3 → 4 → 5 → 6.

Constraints

  • The number of nodes in the tree is in the range [0, 105].
  • -1000 ≤ Node.val ≤ 1000

Visualization

Tap to expand
Minimum Depth of Binary Tree: Find Shortest Path to Leaf3920157Shortest Path:3 → 9Depth = 2Other Paths:3 → 20 → 15 (depth 3)3 → 20 → 7 (depth 3)Output: 2Minimum depth foundThe minimum depth is the shortest distance from root to any leaf node
Understanding the Visualization
1
Input Tree
Binary tree with nodes at different depths
2
Find Paths
Identify all paths from root to leaf nodes
3
Return Minimum
Return the shortest path length
Key Takeaway
🎯 Key Insight: Use BFS to find the first leaf node encountered, which guarantees minimum depth
Asked in
Google 35 Amazon 28 Microsoft 22 Apple 18
89.2K Views
Medium Frequency
~15 min Avg. Time
2.8K 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