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

111. Minimum Depth of Binary Tre

作者: xiaoyaook | 来源:发表于2017-11-11 21:28 被阅读0次

    给一个二叉树,找到最小深度
    分几种情况,

    1. 如果树为空,则返回0
    2. 如果只存在左子树或者只存在右子树,则返回值应为左子树或者右子树的(最小深度+1)
    3. 如果根节点的左子树和右子树都存在,则返回值为(左右子树的最小深度的较小值+1)
    class Solution:
        def minDepth(self, root):
            if root == None:
                return 0
            if root.left == None and root.right != None:
                return self.minDepth( root.right ) + 1
            if root.left != None and root.right == None:
                return self.minDepth( root.left ) + 1
            return min( self.minDepth( root.left ), self.minDepth( root.right ) ) + 1
    

    相关文章

      网友评论

          本文标题:111. Minimum Depth of Binary Tre

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