public boolean isBalanced(TreeNode root) {
if (root == null) {
return true;
}
int leftlength=height(root.left);
int rightlength=height(root.right);
if( Math.abs(leftlength - rightlength) <= 1 && isBalanced(root.left) && isBalanced(root.right)){return true;}
return false;
}
public int height(TreeNode root) {
if (root == null) {
return 0;
}
int rootleft=height(root.left);
int rootright=height(root.right);
return Math.max(rootleft, rootright) + 1;
}
用Java判断是否是平衡二叉树
最新推荐文章于 2024-05-31 10:46:10 发布