二叉搜索树(hdu 3791)

特别基础的题……


#include<bits/stdc++.h>
using namespace std;
int cnt;
char a[15],b[15],c[15];
typedef struct tree
{
    int num;
    tree *lc;
    tree *rc;
} tree;
tree *root;
tree *Insert(tree *s,int x)
{
    tree *t;
    if(s==NULL)
    {
        t=new tree;
        t->lc=NULL;
        t->rc=NULL;
        t->num=x;
        s=t;
    }
    else
    {
        if(x<= s->num)
            s->lc=Insert(s->lc,x);
        else
            s->rc=Insert(s->rc,x);
    }
    return s;
}
void libian(tree *root)
{
    if(root)
    {
        b[cnt++]=root->num+'0';
        libian(root->lc);
        libian(root->rc);
    }
}
void Libian(tree *root)
{
    if(root)
    {
        c[cnt++]=root->num+'0';
        Libian(root->lc);
        Libian(root->rc);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)==1&&n)
    {
        cnt=0;
        root=NULL;
        scanf("%s",a);
        for(int i=0; a[i]!='\0'; i++)
            root=Insert(root,a[i]-'0');
        libian(root);
        b[cnt]='\0';
        while(n--)
        {
            cnt=0;
            root=NULL;
            scanf("%s",a);
            for(int i=0; a[i]!='\0'; i++)
                root=Insert(root,a[i]-'0');
            Libian(root);
            c[cnt]='\0';
            if(strcmp(b,c)==0) printf("YES\n");
            else printf("NO\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41061455/article/details/80218153