4
头图

Depth of binary tree

Title description

Enter a binary tree and find the depth of the tree. The nodes (including roots and leaf nodes) passing through from the root node to the leaf node in turn form a path of the tree, and the length of the longest path is the depth of the tree.

topic link : the depth of the binary tree

Code

/**
 * 标题:二叉树的深度
 * 题目描述
 * 输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。
 * 题目链接:
 * 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) {

    }
}
[Daily Message] In a new day, smile, work hard, and face the sun.

醉舞经阁
1.8k 声望7.1k 粉丝

玉树临风,仙姿佚貌!