需要考虑这种情况:

111. Minimum Depth of Binary Tree刷题笔记_深度优先

# 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

111. Minimum Depth of Binary Tree刷题笔记_算法_02


阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6