二叉排序树



注意: 建立好二叉排序树之后,只需判断两个二叉排序树的前序遍历是否相同即可。




<pre name="code" class="cpp">#include<bits/stdc++.h>
using namespace std;

typedef struct node
{
    char data;
    struct node * lchild;
    struct node * rchild;
}node,*Tree;

char a[1001],b[1001],st1[1001],st2[1001];
int i,j;
Tree creat(Tree &t,char x)
{
    if(t==NULL)
    {
        t=new(node);
        t->data=x;
        t->lchild=NULL;
        t->rchild=NULL;
    }
    else
    {
        if(x<t->data)
        {
            t->lchild=creat(t->lchild,x);
        }
        else
        {
            t->rchild=creat(t->rchild,x);
        }
    }
    return t;
}

void xian(Tree &t)
{

    if(t!=NULL)
    {
        a[i++]=t->data;
        xian(t->lchild);
        xian(t->rchild);
    }
}

void xiannext(Tree &t)
{

    if(t!=NULL)
    {
        b[j++]=t->data;
        xiannext(t->lchild);
        xiannext(t->rchild);
    }
}

int main()
{
    int n;
    Tree t1,t2;
    while(cin>>n&&(n!=0))
    {
        cin>>st1;
        t1=NULL;
        for( i=0;st1[i]!='\0';i++)
        {
            t1=creat(t1,st1[i]);
        }
        i=0;
        xian(t1);
        a[i]='\0';
        while(n--)
        {
            cin>>st2;
            t2=NULL;
            for( j=0;st2[j]!='\0';j++)
            {
                 t2=creat(t2,st2[j]);
            }
            j=0;
            xiannext(t2);
            b[j]='\0';
            if(strcmp(a,b))
            {
                cout<<"NO"<<endl;
            }
            else
            {
                cout<<"YES"<<endl;
            }

        }
    }

}



                
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值