Skip to content
EasyTreesAI interview only

Minimum Depth of Binary Tree

Asked atamazonmetamicrosoftgooglebloomberg

01 · Problem

Given the root of a binary tree, find its minimum depth: the number of nodes along the shortest path that starts at the root and ends at a leaf (a node with no children).

Return 0 for an empty tree. The tree is given in level-order, with null marking a missing child. Note that a node with only one child is not a leaf, so a path cannot stop there.

02 · Examples

Example 01
    3
   / \
  9  20
    /  \
   15   7
Input
root = [3,9,20,null,null,15,7]
Output
2

Node 9 is a leaf at depth 2, which is shallower than leaves 15 and 7 at depth 3.

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

The tree is a chain leaning right; the only leaf is 6, five nodes from the top.

Example 03
Input
root = []
Output
0

An empty tree has depth 0.

03 · Constraints

  • 01The number of nodes in the tree is in the range [0, 100].
  • 02-1000 <= Node.val <= 1000
  • 03A leaf is a node whose left and right children are both null.
  • 04The tree height is at most 100, and the tree may be fully skewed.

04 · Optimal complexity

Time
O(n)
Space
O(n)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.