leetcode——111. 二叉树的最小深度

0.。。。。

class Solution(object):
    def minDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if not root: return 0
        left = self.minDepth(root.left) 
        right = self.minDepth(root.right) 
        return left + right  + 1 if (left == 0 or right == 0) else min(left, right) + 1 
执行用时 :40 ms, 在所有 python 提交中击败了58.03%的用户
内存消耗 :14.6 MB, 在所有 python 提交中击败了39.41%的用户
——2019.11.15
 

复习:
public int minDepth(TreeNode root) {  //根节点到叶子节点的最小节点数
        if(root == null){
            return 0;
        }
        if(root.left == null && root.right == null) {
            return 1;
        }else if(root.left == null || root.right == null){
            return 1+Math.max(minDepth(root.left),minDepth(root.right));
        }else{
            return 1+Math.min(minDepth(root.left),minDepth(root.right));
        }
    }

 

 

——2020.7.2

posted @ 2019-11-15 11:09  欣姐姐  阅读(196)  评论(0编辑  收藏  举报