题目

牛客网

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

解题思路

  1. 深度优先遍历
public int TreeDepth(TreeNode root) {
    int[] max = {0};
    depth(root, max, 1);
    return max[0];
}

private void depth(TreeNode root, int[] max, int curDepth) {
    if (root == null) return;

    if (curDepth > max[0]) max[0] = curDepth;

    depth(root.left, max, curDepth + 1);
    depth(root.right, max, curDepth + 1);
}

results matching ""

    No results matching ""