二叉排序树的判定

二叉排序树的判定_第1张图片

#include
#include
#include
typedef struct node
{
    int data;
    struct node*lchild;
    struct node*rchild;
}BiTNode,*BiTree;
void creat(BiTree*root)
{
    int ch;
    scanf("%d",&ch);
    if(ch==-1)
    {
    *root=NULL;
    }
    else
    {
    *root=(BiTree)malloc(sizeof(BiTNode));
    (*root)->data=ch;
    creat(&((*root)->lchild));
    creat(&((*root)->rchild));
    }
}
int judge(BiTree root)
{
    int pre=0;//pre中序访问节点的前驱
    int a,b;
    if(root==NULL)
        return 1;
    else
    {
        a=judge(root->lchild);
        if(a==0||pre>= root->data)
        return 0;
        else
        {
        pre=root->data;
        b=judge(root->rchild);
        return b;
        }
    }
}
int main()
{
    int n;
    BiTree root;
    creat(&root);
    n=judge(root);
    if(n==1)
    {
        printf("yes\n");
    }
    else printf("no\n");
    return 0;
}

 

你可能感兴趣的:(数据结构)