Minimum Depth of Binary Tree
BFS — Early Exit at First Leaf
LeetCode 111 • Easy • Trees
Input: root = [3, 9, 20, null, null, 15, 7] → Output: 2
The minimum depth is the shortest path from root to the nearest leaf node. BFS guarantees the first leaf found is the shallowest.
Time
O(n)
worst-case visits all nodes
Space
O(n)
queue holds widest level
Visited: 0/5
Queue: 0
Depth: 0
Processing
In Queue
Processed
Leaf Found!
Edge highlight
Queue
empty
Depth
0
Ready
Press Play to watch BFS find the shallowest leaf, or Step to advance one operation at a time.
Unlike max depth, BFS returns immediately when it hits the first leaf — no need to traverse the entire tree.
Unlike max depth, BFS returns immediately when it hits the first leaf — no need to traverse the entire tree.