首页 > 其他 > 详细

JZ38 二叉树的深度

时间:2021-08-06 17:30:58      阅读:15      评论:0      收藏:0      [点我收藏+]

原题连接


描述

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


示例1

输入:{1,2,3,4,5,#,6,#,#,7}
返回值:4

思路

二叉树递归求高度。


解答

public class Solution {
    
    public int TreeDepth(TreeNode root) {
        int deep = 0;
        if (root==null) return 0;

        return 1 + Math.max(TreeDepth(root.left),TreeDepth(root.right));
    }
}

JZ38 二叉树的深度

原文:https://www.cnblogs.com/klaus08/p/15108079.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!