需要考虑这种情况:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def minDepth(self, root: Optional[TreeNode]) -> int:
if not root:
return 0
if not root.left and not root.right:
return 1
minD = 10**9
if root.left:
minD = min(self.minDepth(root.left),minD)
if root.right:
minD = min(self.minDepth(root.right),minD)
return minD+1