- 计算树的高度
计算的是点,不是边
class Solution {
public int height(TreeNode root){
if(root==null) return 0;
int left=height(root.left);
int right=height(root.right);
return Math.max(left,right)+1;
}
}
- 计算树的深度
class Solution {
public void depth(TreeNode root,int d){
if(root.left!=null) depth(root.left,d+1);
if(root.right!=null) depth(root.right,d+1);
}
}