使用递归比较左右子树的高度,该树的最大高度为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;
}
}
本文介绍了一种通过递归方式计算二叉树最大深度的方法。具体实现上,算法对比左右子树的深度,并返回较大者加一。此递归算法简洁高效,适用于计算机科学中的数据结构学习。
495

被折叠的 条评论
为什么被折叠?



