题解 | #二叉树的深度#
二叉树的深度
https://www.nowcoder.com/practice/435fb86331474282a3499955f0a41e8b
import java.util.*;
/**
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;
}
TreeNode node = root;
int depth = 1;
return depth(node, depth);
}
public int depth(TreeNode root, int depth) {
if(root == null) {
return depth - 1;
}
int left = depth(root.left, depth + 1);
int right = depth(root.right, depth + 1);
return left > right ? left : right;
}
}
查看22道真题和解析