840. Minimum Depth of Binary Tree

EasyTreesBinary TreeDFSBFS

Given the root of a binary tree, return its minimum depth — the number of nodes along the shortest path from the root to its nearest leaf. A leaf is a node with no children. An empty tree has depth 0. The tree is given as a level-order array with null for missing children.

Input: A level-order array of the tree.

Output: Integer — the minimum depth.

Examples

Example 1
Input: [1,2,3,4,5,null,6]
Output: 3
Explanation: Nearest leaf is at depth 3.
Example 2
Input: [1,2]
Output: 2
Explanation: One child path.
Example 3
Input: []
Output: 0
Explanation: Empty tree.

Constraints

Asked by

BloombergMicrosoftGoogleMetaAmazon
Solve this problem in the editor →