Problem
Return the minimum depth of a binary tree: the number of nodes on its shortest root-to-leaf path. A leaf has neither a left nor a right child. A missing child of an internal node does not end a path. Return 0 for an empty tree.
int minDepth(TreeNode* root);
TreeNode contains val, left, and right; example brackets show level-order structure.
Examples
Input: root = [9,4,12,null,6]
Output: 2
Node 12 is a leaf two nodes away from the root when counting both endpoints.
Input: root = [3,null,5,null,8,null,13]
Output: 4
Constraints
-
The tree has between 0 and 100000 nodes.
-
Values are between -1000 and 1000, inclusive.