1.算法思想
根据二叉平衡树的性质,先判断递归判断左右子树是否为二叉平衡树,随后判断左右子树的高度差是否大于2
2.实现代码
int IsBalance(BiTree T){
//空树为二叉平衡树
if(T==NULL) return 1;
//递归判断左右子树是否均为二叉平衡树
int lbalance = IsBalance(T->lchild);
int rbalance = ISBalance(T->rchild);
if(rbalance == 0 || lbalance == 0)
return 0;
//获取左右子树高度
int lheight = GetHeight(T->lchild);
int rheight = GetHeight(T->rchild);
//判断左右子树高度差是否小于2
if(abs(lheight-rheight) > 2)
return 0;
else
return 1;
}