public boolean IsBalanced_Solution(TreeNode root) {
if(root==null)return true; //空结点是平衡的
int left=treedepth(root.left); //左子树的深度
int right=treedepth(root.right); //右子树的深度
if(Math.abs(left-right)>1)return false; //高度差是否大于1
return IsBalanced_Solution(root.left)&&IsBalanced_Solution(root.right);//递归判断左右子树是否是平衡的
}
public int treedepth(TreeNode root){
if(root==null) return 0;
int left=treedepth(root.left);
int right=treedepth(root.right);
return left>=right?left+1:right+1;
}
判断是否为平衡二叉树
最新推荐文章于 2022-07-01 15:44:51 发布