Minimum Depth of Binary Tree

Easy
Solve this question on LeetCode

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.

A leaf is a node with no children.

Example

Example 1:
Input: root = [3,9,20,null,null,15,7]
Output: 2
Explanation: The minimum depth is 2 (path: 3 -> 9 or 3 -> 20)

Example 2:
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
Explanation: The minimum depth is 5 (path: 2 -> 3 -> 4 -> 5 -> 6)

Explanation

There are two main approaches to solve this problem: BFS (Breadth-First Search) and DFS (Depth-First Search).

Approach 1: BFS (Recommended)

BFS is the natural choice for finding the minimum depth because it explores nodes level by level. As soon as we encounter a leaf node, we know we've found the minimum depth:

  1. Start at the root with depth = 1
  2. Use a queue to process nodes level by level
  3. For each node, check if it's a leaf (no left and no right child)
  4. If leaf found, return current depth
  5. Otherwise, add children to queue and continue

This approach stops as soon as we find the first leaf node, making it optimal for finding minimum depth.

Approach 2: DFS (Recursive)

DFS recursively explores all paths and keeps track of the minimum:

  1. Base case: if node is null, return 0
  2. If node is a leaf (no children), return 1
  3. If only one child exists, recurse on that child only
  4. If both children exist, return 1 + min(depth of left, depth of right)

DFS must explore all paths to find the minimum, which can be less efficient for wide trees.

Idea Map

Start at root

Add root to queue with depth = 1

While queue not empty:
node, depth = queue.pop()

Is node a leaf?
If yes → return depth

Add children to queue with depth + 1

Return minimum depth

Complexity Analysis

Time Complexity

O(n) - In worst case, we visit all nodes. BFS stops early if a leaf is found near the root.

Space Complexity

O(n) - BFS queue can hold up to O(n) nodes in worst case (for a balanced tree, it's O(w) where w is max width).

Code


from collections import deque
Import deque for efficient queue operations (O(1) append/pop from both ends).
def minDepth(root):
Main function that takes the root of a binary tree.
if not root:
Edge case: empty tree has depth 0.
return 0
Return 0 for null root.
queue = deque([(root, 1)])
Initialize queue with root node and depth 1. Each element is a (node, depth) tuple.
while queue:
Process nodes level by level until queue is empty.
node, depth = queue.popleft()
Dequeue the next node and its depth from the front of the queue.
if not node.left and not node.right:
Check if this node is a leaf (has no children). This is our target!
return depth
Found a leaf! Return current depth immediately - this is the minimum.
if node.left:
If left child exists, add it to the queue.
queue.append((node.left, depth + 1))
Add left child with incremented depth to the queue.
if node.right:
If right child exists, add it to the queue.
queue.append((node.right, depth + 1))
Add right child with incremented depth to the queue.

public int minDepth(TreeNode root) {
Main method that takes root node and returns minimum depth.
if (root == null) {
Edge case: empty tree.
return 0;
Return 0 for null root.
}
End of null check.
Queue<TreeNode> queue = new LinkedList<>();
Create a queue using LinkedList which implements Queue interface.
queue.offer(root);
Add root to queue using offer() method.
int depth = 1;
Start with depth 1 (root level).
while (!queue.isEmpty()) {
Continue while queue has nodes to process.
int levelSize = queue.size();
Get number of nodes at current level for level-by-level processing.
for (int i = 0; i < levelSize; i++) {
Process all nodes at current level.
TreeNode node = queue.poll();
Remove node from front of queue.
if (node.left == null && node.right == null) {
Check if this node is a leaf (no children).
return depth;
Found a leaf! Return current depth - this is the minimum.
}
End of leaf check.
if (node.left != null) queue.offer(node.left);
Add left child to queue if it exists.
if (node.right != null) queue.offer(node.right);
Add right child to queue if it exists.
}
End of level processing loop.
depth++;
Increment depth after processing all nodes at current level.
}
End of while loop.
return depth;
Return depth (should never reach here for valid trees).
}
End of method.

int minDepth(TreeNode* root) {
Main function that takes root pointer and returns minimum depth.
if (!root) {
Check for null root (empty tree).
return 0;
Return 0 for empty tree.
}
End of null check.
queue<pair<TreeNode*, int>> q;
Create queue of (node, depth) pairs using STL queue.
q.push({root, 1});
Push root with initial depth 1.
while (!q.empty()) {
Continue while queue has elements.
auto [node, depth] = q.front();
Get front element with structured binding (C++17).
q.pop();
Remove front element from queue.
if (!node->left && !node->right) {
Check if node is a leaf (no left and no right child).
return depth;
Found a leaf! Return current depth - minimum found.
}
End of leaf check.
if (node->left) q.push({node->left, depth + 1});
Add left child with incremented depth if it exists.
if (node->right) q.push({node->right, depth + 1});
Add right child with incremented depth if it exists.
}
End of while loop.
return 0;
Return 0 (should never reach for valid trees).
}
End of function.

Edge Cases & Common Mistakes

⚠️

Skewed trees (single-child nodes)

If a node has only one child, you must go through that child. Don't just return 1 for a root with one child - the path must end at a leaf. This is the most common mistake!

💡

Empty tree handling

Always check if root is null at the beginning. Return 0 for an empty tree.

BFS vs DFS trade-off

BFS is optimal for finding minimum depth because it stops at the first leaf. DFS must explore all paths to find minimum, which is O(n) in all cases.

📝

Root-only tree

A tree with just the root has minimum depth of 1. The root itself is a leaf (no children).

Understanding BFS vs DFS

BFS and DFS are fundamental tree traversal strategies with different use cases. BFS finds shortest paths naturally, while DFS is simpler to implement recursively. Understanding when to use each is crucial for tree problems.

Learn more about BFS vs DFS →
lightbulb

Disclaimer: This problem is for educational purposes. Practice the 'Minimum Depth of Binary Tree' problem on LeetCode.