平衡二叉树的判定

来源:百度知道 编辑:UC知道 时间:2024/05/16 14:47:12
课程设计的代码

先根据结果建立一个相应的二叉树..然后用下面的函数应该可以了!!!
int depth_bt(struct tree *t) /*求二叉树深度*/
{
if(t==NULL) return 0;
return 1+max(depth_bt(t->lchild),depth_bt(t->rchild));
}
int balance(struct tree *t) /*递归[判断是不是平衡二叉树*/
{ int bl,br;
if(t==NULL) return 1; /*空树输出是平衡二叉树*/
bl=depth_bt(t->lchild); /*将左子树的深度赋值给bl*/
br=depth_bt(t->rchild); /*将右子树的深度赋值给br*/
if(bl-br>1||br-bl>1)return 0; /*如果不满足平衡二叉树的定义返回错误信息*/
return balance(t->lchild)&&balance(t->rchild); /*递归调用*/
}