树的深度=左子树深度与右子树深度的较大值+1

二叉树的深度

http://www.nowcoder.com/questionTerminal/435fb86331474282a3499955f0a41e8b

树相关问题,肯定是递归求解的。递归思路:根节点如何与左右子树关联起来。

树的深度=Math.max(DeepHeight(root.left),DeepHeight(root.right))+1

大佬的图解,来源:https://leetcode-cn.com/problems/er-cha-shu-de-shen-du-lcof/solution/mian-shi-ti-55-i-er-cha-shu-de-shen-du-xian-xu-bia/.
图片说明

    /**
     * 输入一棵二叉树,求该树的深度。
     * 从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,
     * 最长路径的长度为树的深度。
     * @param root 一棵二叉树
     * @return 树的深度
     */

    public int TreeDepth(TreeNode root) {
        if(root==null){
            return 0;
        }
        return Math.max(TreeDepth(root.left),TreeDepth(root.right))+1;

    }
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-03 18:22
投了几百份简历,专业和方向完全对口,都已读不回。尝试改了一下学校,果然有奇效。
steelhead:这不是很正常嘛,BOSS好的是即便是你学院本可能都会和聊几句,牛客上学院本机会很少了
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
今天 17:10
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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