题解 | #二叉树的最大深度#

二叉树的最大深度

http://www.nowcoder.com/practice/8a2b2bf6c19b4f23a9bdb9b233eefa73

递归比较左右子树即可。

import java.util.*;
public class Solution {
    public int maxDepth (TreeNode root) {
        if(root==null)return 0;
        if(root.left==null&&root.right==null)return 1;
        int leftDepth =  maxDepth(root.left)+1;
        int rightDepth =  maxDepth(root.right)+1;
        return leftDepth>rightDepth?leftDepth:rightDepth;
    }
}
全部评论

相关推荐

12-13 14:51
已编辑
井冈山大学 算法工程师
龙虾x:算法比你强的没有你美,比你美的…..算了已经没有比你美的了
工作两年想退休了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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