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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code