Minimum Depth of Binary Tree

EasyTreeslevel order

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Given 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**. A leaf is a node with no children. A node that has only one child is not a leaf, so a missing child does not end a path. Return 0 for an empty tree.

Examples

Input: root = [3,9,20,null,null,15,7]
Output: 2
The path 3 -> 9 reaches a leaf after 2 nodes.
Input: root = [2,null,3,null,4,null,5,null,6]
Output: 5
The tree is a single right-leaning chain; its only leaf is 6.

Constraints

  • 0 <= number of nodes <= 10^4
  • -1000 <= Node.val <= 1000

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More level order problems