首页

源码搜藏网

首页 > 开发教程 > 软件工程 >

剑指offer(四十五)之二叉树的深度

创建时间:2016-06-09 20:23  

题目描述

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

代码:

<span style="color:#993399;">public class Solution {
    public int TreeDepth(TreeNode pRoot){
        if(pRoot==null)
            return 0;
        return getDepth(pRoot,0);
    }
     
    private int getDepth(TreeNode root,int depth){
        if(root==null)
            return depth;
        depth++;
        return Math.max(getDepth(root.left,depth),getDepth(root.right,depth));
    }
}</span>


0
0
   
上一篇:使用Aspose组件将WORD、PDF、PPT转为图片
下一篇:Java源码之LinkedList

相关内容

热门推荐