111. Minimum Depth of Binary Tree

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))
        
    原文作者:oo上海
    原文地址: https://www.jianshu.com/p/04b9509a34ff
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞