美文网首页
111. Minimum Depth of Binary Tre

111. Minimum Depth of Binary Tre

作者: GoDeep | 来源:发表于2018-04-27 16:18 被阅读0次
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 Tre

      本文链接:https://www.haomeiwen.com/subject/blfilftx.html