树的高度等于左右子树的最大高度。没啥好说的。
import java.util.*;
public class Solution {
/**
*
* @param root TreeNode类
* @return int整型
*/
public int maxDepth (TreeNode root) {
// write code here
return maxDepth(root,0);
}
public int maxDepth (TreeNode root,int n) {
if(null==root)
return n;
return Math.max(maxDepth(root.left,n+1),maxDepth(root.right,n+1));
}
}