/111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree

Easy
Trees52.5% acceptance

Given the root node of a binary tree, return the minimum depth of the tree. The minimum depth is defined as the number of nodes along the shortest path from the root node to the nearest leaf node. A leaf node is a node with no children.

Example 1

Input: TreeNode(5, TreeNode(2), TreeNode(8, None, TreeNode(9)))

Output: 2

Explanation: The shortest path from root (5) to leaf (2) is of length 2.

Example 2

Input: TreeNode(1, None, TreeNode(2, None, TreeNode(3, None, TreeNode(4))))

Output: 4

Explanation: The shortest path from root (1) to leaf (4) is of length 4.

Constraints

  • The number of nodes in the tree is in the range [0, 105].
  • -1000 <= Node.val <= 1000
Python (current runtime)

Case 1

Input: TreeNode(10, TreeNode(6, TreeNode(4), TreeNode(7)), TreeNode(15))

Expected: 2

Case 2

Input: TreeNode(1)

Expected: 1

Case 3

Input: TreeNode(0, None, TreeNode(-1, None, TreeNode(-2)))

Expected: 3

Case 4

Input: None

Expected: 0