寻找二叉树最长路径

递归的方法

1.节点为null时,返回深度为0
2.节点的左右子节点中有一个为null时,返回1+maxDepth(root.left(or root.right))
3.若节点的左右子节点都不为null时,返回两者中大的那一个。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public int maxDepth(TreeNode root) {
if(root==null){
return 0;
}else if(root.left!=null&& root.right!=null){
return max(1+maxDepth(root.left),1+maxDepth(root.right));
}else if(root.left==null){
return 1+maxDepth(root.right);
}else{
return 1+maxDepth(root.left);
}
}

public int max(int a,int b){
return (a>b?a:b);
}