2Brute2Best
πŸ”₯0

Minimum Depth of Binary Tree

EasyΒ·LeetCode #111Basic Anchor
LeetCode
30-Second Mental ModelTree BFS: shortest distance (minimum depth)
Complexity:O(N)β†’O(N)
Key Insight

Go through the tree one level at a time with a queue. The first leaf we pop is the closest one, because levels come out from shallow to deep. Return the round number at that moment. Nothing deeper can beat it.

Core Validity Invariant

The first leaf popped from the queue is at the smallest depth of any leaf, so its round number is the minimum depth.

01

Problem

You are given the root of a binary tree. Return its minimum depth.

The depth of a node is the number of nodes on the path from the root down to it, so the root has depth 1. A leaf is a node with no children. The minimum depth is the depth of the closest leaf.

A node with only one child is not a leaf, so the walk cannot stop there.

Input

The root of a binary tree. The tree can be empty.

Output

One number: the minimum depth. An empty tree has depth 0.

Example 1
Input: root = [3, 9, 20, null, null, 15, 7]
Output: 2

Node 9 is a leaf at depth 2 (the path is 3 β†’ 9). That is the closest leaf.

Example 2
Input: root = [2, null, 3, null, 4, null, 5, null, 6]
Output: 5

The tree is one chain: 2 β†’ 3 β†’ 4 β†’ 5 β†’ 6. The only leaf is 6, at depth 5. Node 2 has only one child, so it is not a leaf.

Constraints
  • β€’ 0 ≀ number of nodes ≀ 10^5
  • β€’ βˆ’1000 ≀ Node.val ≀ 1000
02

First, think yourself

Before opening any solution, try to answer these on paper. Use hints only when stuck β€” each hint is counted.

  1. 1.

    Max Depth asks for the deepest node. This asks for the closest leaf. What changes in the code?

  2. 2.

    Is a node with only one child a leaf? What should the walk do at that node?

  3. 3.

    If I go level by level, which leaf do I meet first? Is that the closest one?

  4. 4.

    Do I count depth once per node or once per round? What goes wrong if I count per node?

  5. 5.

    When I find a leaf, can a later leaf still be shallower? How would I know?

03

Brute force approach

05

Optimized approach

06

Why does this algorithm work?

07

Dry run

08

All approaches compared

ApproachIdeaTimeSpace
Brute ForceRecurse to every leaf and keep the smallest depth seenO(N)O(H)
OptimalLevel-order queue; return the round number when the first leaf is poppedO(N)O(W)
09

Why does this belong to this pattern?

Clues in the problem statement
  • β€œminimum depth”We want a number of levels (nodes on a path), not a sum and not the path itself.
  • β€œclosest leaf”The nearest end of a route. In a tree, BFS reaches nodes in order of distance, so the first match is the shortest.
  • β€œa leaf has no children”A node with one child is not a stop. The leaf test has to check both children.
Tree, answer is a depth+shortest route to any leaf+level by level (BFS)+stop at the first leaf→Tree BFS: shortest distance (minimum depth)

Maximum Depth (LC 104) asks for the deepest node, so a DFS with 1 + max(left, right) is the natural fit. This question asks for the closest leaf. BFS reaches nodes in order of depth, so the first leaf it pops is the shortest route. The one extra rule is the leaf test: a leaf has no children at all, so a node with one child is not a stop.

10

Don't make this mistake

Copying Maximum Depth: `1 + min(left, right)`
βœ— int minDepth(TreeNode* root) { if (!root) return 0; return 1 + min(minDepth(root->left), minDepth(root->right)); }
βœ“ int minDepth(TreeNode* root) { if (!root) return 0; if (!root->left) return 1 + minDepth(root->right); if (!root->right) return 1 + minDepth(root->left); return 1 + min(minDepth(root->left), minDepth(root->right)); }

This is the pattern-confusion trap. Maximum Depth uses max, and the copy changes it to min. But a missing child returns 0, and min picks that 0. On [2, null, 3, null, 4, null, 5, null, 6], node 2 has no left child, so min(0, …) is 0 and the code returns 1. The correct answer is 5: node 2 has one child, so it is not a leaf, and the walk must go down to 6.

A node with one child counts as a leaf
βœ— if (!node->left || !node->right) return depth; // BFS leaf test
βœ“ if (!node->left && !node->right) return depth;

|| makes a node with one child end the search. On [1, 2, 3, 4, null, null, 5, 6, 7], node 2 has only a left child (4). It is popped first in round 2, so the code returns 2. The correct answer is 3, from leaf 5.

Counting depth per node instead of per round
βœ— while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { depth++; // runs once per node ...
βœ“ while (!q.empty()) { depth++; // once per round, before the for loop int size = q.size(); for (int i = 0; i < size; i++) {

depth has to be the level number. Increasing it per node makes a level with two nodes add 2. On [1, 2, 3, 4, null, null, 5, 6, 7], leaf 5 is the 5th node popped, so the code returns 5 instead of 3.

Returning the first leaf that DFS meets
βœ— int dfs(TreeNode* node, int depth) { // call with depth = 1 for the root if (!node) return 0; if (!node->left && !node->right) return depth; int left = dfs(node->left, depth + 1); if (left) return left; return dfs(node->right, depth + 1); }

A left-first DFS takes the first leaf it reaches, not the closest one. On [1, 2, 3, 4, null, null, 5, 6, 7] it goes 1 β†’ 2 β†’ 4 β†’ 6 and returns 4. Leaf 5 at depth 3 is the answer. Only a level-by-level walk meets shallow leaves first.

11

Want to implement this?

Write it yourself on LeetCode, then come back and compare your code with the solutions above.

Open coding workspace
How did it go?
Beyond The LeetCode Problem

How this concept connects to System Design & C++ Systems

Bridge DSA with production engineering

Top engineering interviews test whether you understand how algorithms like Tree BFS translate into production architectures and low-level memory mechanics.