您的位置:首页 > 其它

104MaximumDepthofBinaryTree

2016-05-20 15:20 183 查看
使用递归比较左右子树的高度,该树的最大高度为max(max(left), max(right)+1

该题与交换左右子树类似
public class Solution {
public int maxDepth(TreeNode root) {
int lmax, rmax;
if(root == null) {
return 0;
}
lmax = maxDepth(root.left);
rmax = maxDepth(root.right);
if(lmax >= rmax)
return lmax+1;
else
return rmax+1;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: