剑指Offer-二叉树-(8)

知识点/数据结构:二叉树

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

思路:递归的思路

public class Solution {
    public int TreeDepth(TreeNode root) {
        //2018.10.07找了大王八取经,梦想还是要有的。
                if(root==null){
            return 0;
        }
           
        int nLelt=TreeDepth(root.left);
        int nRight=TreeDepth(root.right);
         
        return nLelt>nRight?(nLelt+1):(nRight+1);
    }
}

猜你喜欢

转载自blog.csdn.net/qq_35649064/article/details/84874069