二叉排序树

来源:互联网 发布:炫踪网络上市计划启动 编辑:程序博客网 时间:2024/06/06 02:25

此题高能预警

题目链接

#include<iostream>#include<algorithm>#include<string>#include<cstring>using namespace std;typedef struct node{    char data;    node *l;    node *r;}Tree;Tree *creat(char key, Tree *root){    if(root==NULL)    {        root=new Tree;        root->l=NULL;        root->r=NULL;        root->data=key;    }    else    {        if(key<root->data)            root->l=creat(key, root->l);        else            root->r=creat(key, root->r);    }    return root;}int f=0;char a[1000];void fistree(Tree *root){    if(root)    {        a[f++]=root->data;        fistree(root->l);        fistree(root->r);    }}char b[1000];int l=0;void lastree(Tree *root){    if(root)    {        lastree(root->l);        lastree(root->r);        b[l++]=root->data;    }}int main(){    ios::sync_with_stdio(false);    int n;    char x[100];    Tree *root;    while(cin>>n)    {        if(n==0)            break;        cin>>x;        int len=strlen(x);        root=NULL;        for(int i=0;i<len;i++)        {            root=creat(x[i], root);        }        fistree(root);        lastree(root);        a[f]='\0';        b[l]='\0';        while(n--)        {            char c[100];            cin>>c;            if(strcmp(a,c)==0)                cout<<"YES"<<endl;            else if(strcmp(b, c)==0)                cout<<"YES"<<endl;            else if(strcmp(x, c)==0)                cout<<"YES"<<endl;            else cout<<"NO"<<endl;        }    }    return 0;}


0 0
原创粉丝点击