Skip to content

Minimum Depth of Binary Tree

Find the shortest root-to-leaf path with BFS, which can stop at the very first leaf it meets.

EasyTree BFS
Solve on LeetCode

The problem

You get the root of a binary tree. Return its minimum depth: the number of nodes on the shortest path from the root down to a leaf, where a leaf is a node with no children. An empty tree has depth 0.

Input root = [8, 3, 10, null, null, 6, 14, null, 7]

Output 2

3 has no children, so the path 8, 3 with two nodes is the shortest one.

Input root = [5, 4, null, 2, 7]

Output 3

5 has only a left child, so it is not a leaf; the nearest leaves, 2 and 7, are three nodes down.

Key constraint: Up to 100,000 nodes; BFS can stop at the first leaf instead of visiting the whole tree.

How to spot the pattern

  • You want the shortest path from the root to a leaf, and BFS reaches nodes in order of depth.
  • The first leaf found level by level is the answer, so you can stop early.

The idea

BFS explores the tree one level at a time, so the first leaf it reaches lies on the shallowest level that has any leaf. Count levels as you go and return the current depth the moment you pop a node with no children. On a lopsided tree with a leaf near the top, this stops long before visiting every node.

Brute force: Recursively find the depth of every leaf and take the smallest, which always visits all n nodes.

Step by step

  1. 1If the root is null, return 0.
  2. 2Put the root in a queue and set depth = 1.
  3. 3Process the queue one level at a time; for each node, return depth if it has no children, otherwise push its non-null children.
  4. 4After finishing a level, add 1 to depth.

See the pattern move

The tree bfs pattern on a small example. Step through it, or press play.

Code

from collections import deque


class Solution:
    def minDepth(self, root: Optional[TreeNode]) -> int:
        if root is None:
            return 0
        queue = deque([root])
        depth = 1
        while queue:
            for _ in range(len(queue)):  # one level
                node = queue.popleft()
                if node.left is None and node.right is None:
                    return depth  # the first leaf is the shallowest one
                if node.left:
                    queue.append(node.left)
                if node.right:
                    queue.append(node.right)
            depth += 1
        return depth

Complexity

Time

O(n)

Space

O(w)

In the worst case every node is visited, but BFS stops at the first leaf; w is the largest number of nodes on one level, which bounds the queue.

Watch out for

  • Treating a missing child as a leaf: 1 + min(depth(left), depth(right)) returns 1 for a root that has only one child, which is wrong.
  • Forgetting the empty tree, which has depth 0.
  • Checking for a leaf only when you push a node and never for the root, so a single-node tree is missed.

Follow-up questions

How would you write it with DFS?

Recurse, but when one child is null, use only the other child's depth: 1 + depth(other); take the minimum only when both children exist. It has to visit the whole tree.

What if you need the leaf itself, not just its depth?

Return the node you popped when you found the first leaf; because BFS goes left to right, it is also the leftmost leaf on that level.

In Hinglish

BFS level by level chalta hai, toh jo pehla leaf mile wahi sabse kam gehrai par hai. Wahin ruk jao aur depth return kar do.

Related problems