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

二叉树的最大深度

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

实现原理:递归,比较出最大深度

public class Solution {
    /**
     * 
     * @param root TreeNode类 
     * @return int整型
     */
    public int maxDepth (TreeNode root) {
        // write code here
        if (root==null)
            return 0;
        //递归
        int dl = maxDepth(root.left)+1;
        int dr = maxDepth(root.right)+1;
        //比较深度大小
        if (dl>dr)
            return dl;
        else
            return dr;
    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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