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


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

所以根据这一条 判断即可

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;
}

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM