111. Minimum Depth of Binary Tree

111. Minimum Depth of Binary Tree_第1张图片
image.png

自己想一下递归过程,就知道怎么递推了

class Solution:
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root: return 0
        if not root.left and not root.right: return 1
        if not root.left: return 1+self.minDepth(root.right)
        if not root.right: return 1+self.minDepth(root.left)
        return 1+min(self.minDepth(root.left), self.minDepth(root.right))
        

你可能感兴趣的:(111. Minimum Depth of Binary Tree)