题目描述
输入一棵二叉树,判断该二叉树是否是平衡二叉树。
public class Solution {
public boolean IsBalanced_Solution(TreeNode root) {
if (root == null) {
return true;
}
if (Math.abs(this.TreeDepth(root.left)-this.TreeDepth(root.right))<=1) {
return true;
}
else {
return false;
}
}
public int TreeDepth(TreeNode root) {
if (root == null) {
return 0;
}
int depth;
if (this.TreeDepth(root.left) > this.TreeDepth(root.right)) {
return 1 + this.TreeDepth(root.left);
}
else {
return 1 + this.TreeDepth(root.right);
}
}
}
本文介绍了一种判断二叉树是否为平衡二叉树的方法。通过递归计算每个节点的左右子树深度,并比较其绝对值是否小于等于1来确定整棵树是否平衡。
14万+

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



