4

JZ-038-二叉树的深度

 2 years ago
source link: https://segmentfault.com/a/1190000041188178
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

JZ-038-二叉树的深度

发布于 12 月 27 日

二叉树的深度

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

题目链接: 二叉树的深度

/**
 * 标题:二叉树的深度
 * 题目描述
 * 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
 * 题目链接:
 * https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b?tpId=13&&tqId=11191&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
 */
public class Jz38 {

    /**
     * 递归
     *
     * @param root
     * @return
     */
    public int treeDepth(TreeNode root) {
        return root == null ? 0 : 1 + Math.max(treeDepth(root.left), treeDepth((root.right)));
    }

    public static void main(String[] args) {

    }
}

【每日寄语】 新的一天,要微笑,要努力,要面向阳光。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK