二叉排序树--

#include<bits/stdc++.h>
using namespace std;
int n;
string str[25];
struct node
{
    node *l,*r;
    char data;
}*root;
string a,b,c;
struct node *creat(node *root,char data)
{
    if(root==NULL)
    {
        root=new node;
        root->l=NULL;
        root->r=NULL;
        root->data=data;
    }
    else
    {
        if(data<root->data)
            root->l=creat(root->l,data);
        else
            root->r=creat(root->r,data);
    }
    return root;
}
void pre(node *root)
{
    if(root!=NULL)
    {
        c=root->data;
        a+=c;
        pre(root->l);
        pre(root->r);
    }
}
void last(node *root)
{
    if(root!=NULL)
    {
        last(root->l);
        last(root->r);
        c=root->data;
        b+=c;
    }
}
int main()
{
    while(cin>>n)
    {
        a.clear();
        b.clear();
        root=NULL;
        if(!n)
            break;
        cin>>str[0];
        for(int i=0; i<str[0].size(); i++)
            root=creat(root,str[0][i]);
        pre(root);
        last(root);
        for(int i=1; i<=n; i++)
        {
            cin>>str[i];
            if(str[0]==str[i]||str[i]==a||str[i]==b)
                cout<<"YES"<<endl;
            else
                cout<<"NO"<<endl;
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/BePosit/article/details/82527364