排序二叉树

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2482&cid=1184

 1 #include<cstdio>
 2  #include<cstdlib>
 3  #include<cstring>
 4  using namespace std;
 5  typedef struct node
 6  {
 7      char data;
 8      struct node *l,*r;
 9  } treenode,*tree;
10  char s[30],s1[30],s2[30];
11  int c=0;
12  int insert(tree *t,char x)
13  {
14      treenode *p,*cur,*parent=NULL;
15      cur=*t;
16      while(cur!=NULL)
17      {
18          if(cur->data==x) return 0;
19          parent=cur;
20          if(x<cur->data)
21              cur=cur->l;
22          else
23              cur=cur->r;
24      }
25      p=(treenode *)malloc(sizeof(treenode));
26      p->data=x;
27      p->l=NULL;
28      p->r=NULL;
29      if(!parent)
30          *t=p;
31      else if(x<parent->data)
32          parent->l=p;
33      else
34          parent->r=p;
35      return 1;
36  }
37  void in(tree t)
38  {
39      if(t)
40      {
41          s[c++]=t->data;
42          in(t->l);
43          in(t->r);
44      }
45  }
46  int main()
47  {
48      int tt;
49      while(scanf("%d",&tt)!=EOF)
50      {
51          c=0;
52          if(tt==0) return 0;
53          tree t=NULL;
54          scanf("%s",s2);
55          int k1=strlen(s2);
56          for(int i=0; i<k1; i++)
57          {
58              insert(&t,s2[i]);
59          }
60          in(t);
61          s[c]='\0';
62          strcpy(s1,s);
63          for(int i=1; i<=tt; i++)
64          {
65              c=0;
66              memset(s,0,sizeof(s));
67              tree t1=NULL;
68              scanf("%s",s2);
69              int k2=strlen(s2);
70              for(int j=0; j<k2; j++)
71              {
72                  insert(&t1,s2[j]);
73              }
74              in(t1);
75              s[c]='\0';
76              if(!strcmp(s,s1)) printf("YES\n");
77              else printf("NO\n");
78          }
79      }
80      return 0;
81  }
82  
83  
View Code

 

posted @ 2013-07-29 16:09  null1019  阅读(142)  评论(0编辑  收藏  举报