Problem 257
Minimum Depth of Binary Tree
Easy · 10 pts · Tree, Binary Tree, Breadth-First Search
Problem
You are given a binary tree. Print its minimum depth: the number of nodes on the shortest path from the root down to the nearest leaf. A leaf is a node with no children. Be careful with nodes that have only one child: such a node is not a leaf, so the path must continue into its existing child. Breadth-first search is a natural fit, because the first leaf it reaches is the nearest one.
Input
First line: integer N, the number of tokens. Second line: N tokens describing the tree in level order, where 'null' marks a missing child.
Output
Print the minimum depth.
Constraints
1 <= number of nodes <= 100000; -1000 <= value <= 1000
Example input
7 3 9 20 null null 15 7
Example output
2
Explanation
The root 3 has a leaf child 9, so the path 3 → 9 contains 2 nodes. The leaves under 20 are deeper, so the minimum depth is 2.