【剑指offer】二叉树的深度

谁说胖子不能爱 提交于 2020-01-25 13:11:30

题目描述

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

解题思路

没啥好说的,就觉得自己牛逼极了!

代码

/**
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;
    public TreeNode(int val) {
        this.val = val;
    }
}
*/
public class Solution {
    public int TreeDepth(TreeNode root) {
        if(root == null) return 0;
        return 1 + (TreeDepth(root.left) > TreeDepth(root.right) ? TreeDepth(root.left) : TreeDepth(root.right));
    }
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!