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

111. Minimum Depth of Binary Tre

作者: oo上海 | 来源:发表于2016-07-30 03:12 被阅读1次

111. Minimum Depth of Binary Tree

题目:
https://leetcode.com/problems/minimum-depth-of-binary-tree/

难度:

Easy

注意leaf node反正就是没有left和right的

比如下图

1
 \
  2

2是一个孩子节点

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

相关文章

网友评论

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

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