LeetCode #111 Easy

Minimum Depth of Binary Tree

Minimum Depth of Binary Tree is LeetCode 111 (Easy). Return the number of nodes on the shortest path from the root down to the nearest leaf, a node with no children. An empty tree has depth 0.

The tree can hold up to 100,000 nodes, so a tree shaped like a chain is 100,000 levels deep.

Constraints
  • The number of nodes in the tree is in the range [0, 10⁵].
  • -1000 <= Node.val <= 1000
treebfs
Open on LeetCode ↗
02

Intuition

BFS visits a tree one level at a time: every node at depth 1, then every node at depth 2, and so on. So the first leaf BFS reaches is the shallowest leaf, and its level is the answer. The search stops there and never looks at anything deeper.

The one thing to get right is what counts as a leaf. A path that stops at a node with one child has not reached the bottom of the tree.

  • Leaf: no left child and no right child, so its depth is the answer.
  • Not a leaf: its children that exist go into the queue for the next level.
How to spot this pattern

The shortest path from root to leaf is a nearest-target search, and BFS by levels answers it the moment it reaches the first target. The same shape finds the nearest exit in a maze or the fewest moves in a puzzle.

03

Approach

Try it first

Before reading on, write down what a function should return for a node that has a left child but no right child. Then decide which traversal can stop as soon as it finds the answer. Aim for O(n) time in the worst case and an early exit when a shallow leaf exists.

1

Return 0 for an empty tree

If root is missing there is no path at all, so the depth is 0. Otherwise put the root in a queue and set depth = 1, because the problem counts the nodes on the path, not the edges.

2

Take one level at a time

Note how many nodes the queue holds and pop exactly that many. Those are all the nodes at the current depth; children pushed meanwhile belong to the next level and wait behind them.

3

Return at the first leaf

When a popped node has neither child, return depth. Every shallower level was checked completely and had no leaf, so no leaf can be closer to the root.

4

Queue the children that exist

Otherwise push each child that is present. A node with only one child keeps the search going through that child, which is exactly how it differs from a leaf.

5

Move one level down

When a level ends without a leaf, add 1 to depth and repeat. The loop always ends with a return, because every non-empty tree has at least one leaf.

04

Code in Python, C++ and Java

▶1from collections import deque
▶2 
▶3class Solution:
▶4 def minDepth(self, root):
▶5 if not root:
▶6 return 0
▶7 queue = deque([root])
▶8 depth = 1
▶9 while queue:
▶10 for _ in range(len(queue)):
▶11 node = queue.popleft()
▶12 if not node.left and not node.right:
▶13 return depth
▶14 if node.left:
▶15 queue.append(node.left)
▶16 if node.right:
▶17 queue.append(node.right)
▶18 depth += 1
depth 1depth 2depth 33920157queue3checkingno node yetdepth1root 3 in the queue, depth 1
queue[3]
depth1
The depth counts nodes on the path, so the root alone is depth 1. BFS will take the tree one level at a time, top to bottom, which is why the first leaf it meets is the closest one.
depth 1depth 2depth 33920157queueemptychecking3left9right20depth1node 3 has 2 children → not a leaf
node3depth 1
queue[]
Node 3 has two children, so it is not a leaf. Both children go to the back of the queue for the next level.
depth 1depth 2depth 33920157queue9checking3left9right20depth1queue node 9 for depth 2
queue[9]
Node 9 is the left child of 3, one level deeper, so it goes to the back of the queue. It is checked once depth 1 is finished.
depth 1depth 2depth 33920157queue920checking3left9right20depth1queue node 20 for depth 2
queue[9, 20]
Node 20 is the right child of 3, one level deeper, so it goes to the back of the queue. It is checked once depth 1 is finished.
depth 1depth 2depth 33920157queue920checkinglevel 1 had no leafdepth2no leaf at depth 1 → depth 2
depth2
queue[9, 20]all at depth 2
Every node at depth 1 has been checked and none was a leaf. The queue now holds exactly the nodes at depth 2, so that level is checked next.
depth 1depth 2depth 33920157queue20checking9leftnonerightnonedepth2node 9 has no children → it is a leaf
node9depth 2
queue[20]
Node 9 has neither a left nor a right child, so a path from the root can end here.
depth 1depth 2depth 33920157queue20checking9leftnonerightnonedepth2first leaf at depth 2 → return 2
result2
Every level above depth 2 was checked completely and had no leaf, so no leaf is closer to the root. Return 2. The 2 faded nodes were never visited: BFS stops before reaching them.
05

Recursive DFS

A node with no left child can only continue to the right, so its depth is 1 plus the right subtree's depth, and the same holds the other way round. Only a node with both children takes the smaller of the two. An empty subtree returns 0.

▶1import sys
▶2 
▶3class Solution:
▶4 def minDepth(self, root):
▶5 sys.setrecursionlimit(200000)
▶6 if not root:
▶7 return 0
▶8 if not root.left:
▶9 return 1 + self.minDepth(root.right)
▶10 if not root.right:
▶11 return 1 + self.minDepth(root.left)
▶12 return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
06

Common pitfalls

Swapping max for min in the Maximum Depth formula

✗ Wrong
if not root:
    return 0
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))
✓ Right
if not root.left:
    return 1 + self.minDepth(root.right)
if not root.right:
    return 1 + self.minDepth(root.left)
return 1 + min(self.minDepth(root.left), self.minDepth(root.right))

A missing child returns 0, and min picks it, as if the path could end at a node that still has a child. For [1, 2] this returns 1, but the only leaf is node 2, at depth 2. On the chain [2,null,3,null,4,null,5,null,6] it returns 1 instead of 5.

Stopping at a node with one missing child

✗ Wrong
if not node.left or not node.right:
    return depth
✓ Right
if not node.left and not node.right:
    return depth

With or, the BFS stops at the first node that lacks either child. For [1, 2] it returns 1 at the root, which still has a left child; the answer is 2.

07

Edge cases

Empty tree

Return 0 before touching the queue.

A chain of 100,000 nodes

The BFS queue holds one node at a time, so the chain is no problem. The recursive version makes 100,000 nested calls, which is past Python's default limit of 1,000; its code raises the limit first.

08

Complexity

Time
O(n)
Space
O(n)
In the worst case the only leaves are on the last level, and every node is visited once. The queue holds at most one level plus the children being added, which is about n / 2 nodes for a full tree. When a shallow leaf exists, BFS stops long before n.
09

Minimum Depth vs Maximum Depth

The two problems look like mirror images, but only one of them can reuse the other's formula.

ProblemLooks forRecursive formulaBFS
104 Maximum Depththe deepest leaf1 + max(left, right) works: a missing child gives 0 and max never picks it over a real onemust finish every level
111 Minimum Depth (this page)the shallowest leafneeds a case for one missing child, or min picks the 0stops at the first leaf
10

Minimum Depth of Binary Tree FAQ

Is BFS or DFS better here?

BFS. If a leaf sits at depth 2 next to a branch 50,000 nodes deep, BFS checks a handful of nodes and returns 2, while recursive DFS walks all 50,000 nodes of that branch before it can rule it out. When every leaf is on the last level, the two do the same work.