public class Solution {
public int maxDepth(TreeNode root) {
int count = 0;
if(root==null)
count = 0;
else if(root.left==null && root.right==null)
count = 1;
else{
int lmax = maxDepth(root.left)+1;
int rmax = maxDepth(root.right)+1;
count = Math.max(lmax,rmax);
}
return count;
}
}
树的高度为:左子树的高度与右子树高度二者较大的值,递归求解。