判断是否为二叉排序树 平衡二叉树

中序遍历二叉排序树一定是一个递增序列

所以根据这一条 判断即可

int pre = -INF;
bool check(Bitree T)
{
    if(T == NULL) reutrn true;

    if(check(T->lchild) && T->data > pre)
    {
        pre = T->data;
    }
    else return false;
    return check(T->rchild);
}

平衡二叉树是在二叉排序树的基础上的保证每个点的子树高度差的绝对值小于等于1

这是改了一下王道上的代码

并没有实现判断二叉排序树..........直接判断的平衡......why....

bool check(Bitree T, int &h)
{
    int hl, hr; //左右子树的高度
    if(T == NULL) return true;
    if(T->lchild != NULL && T->rchild != NULL)
    {
        h = 0;
        return true;
    }
    bool b1 = check(T->lchild, hl);
    bool b2 = check(T->rchild, hr);
    h = max(hl, hr) + 1;    //h为当前子树的高度
    if(abs(hl - hr) >= 2) return false;
    return b1 && b2;
}
原文地址:https://www.cnblogs.com/WTSRUVF/p/11205716.html