leetcode 104. 二叉树的最大深度 Maximum Depth of Binary Tree(使用c++/java/python)

https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

https://leetcode.com/problems/maximum-depth-of-binary-tree/

递归。计算左子树高度和右子树高度的较大者。记得+1。

执行用时: c++ 4ms; java 1ms; python 68ms

 

c++

class Solution {
public:
    int maxDepth(TreeNode* root) {
        if(root==NULL)
            return 0;
        return max(maxDepth(root->left),maxDepth(root->right))+1;
    }
};

java

class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null)
            return 0;
        return java.lang.Math.max(maxDepth(root.left),maxDepth(root.right))+1;
    }
}

python

class Solution:
    def maxDepth(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        if root is None:
            return 0
        return max(self.maxDepth(root.left),self.maxDepth(root.right))+1

 

全部评论

相关推荐

小浪_Coding:找硬件测试,也可兼顾软测欧, 简历还可以的 ,注意排版,项目写的有条理一点, 然后个人技能多加点, 润色好简历之后就开始沟通海投了,深圳,东莞这边做硬件相关的公司还不少, 医疗类,仪器类的都可以尝试
点赞 评论 收藏
分享
06-15 18:44
黄淮学院 Java
Lynn012:如果是居民楼还是算了吧,看着有点野呢
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务