-
Notifications
You must be signed in to change notification settings - Fork 0
/
题目1009:二叉搜索树.cpp
96 lines (93 loc) · 1.29 KB
/
题目1009:二叉搜索树.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include<stdio.h>
#include<string.h>
struct Node{
Node *lchild;
Node *rchild;
int c;
}Tree[110];
int loc;
Node *create(){
Tree[loc].lchild=Tree[loc].rchild=NULL;
return &Tree[loc++];
}
char str1[25],str2[25];
int size1,size2;
char *str;
int *size;
void postOrder(Node *T)
{
if(T->lchild!=NULL)
{
postOrder(T->lchild);
}
if(T->rchild!=NULL)
{
postOrder(T->rchild);
}
str[(*size)++]=T->c+'0';
}
void inOrder(Node *T)
{
if(T->lchild!=NULL)
{
inOrder(T->lchild);
}
str[(*size)++]=T->c+'0';
if(T->rchild!=NULL)
{
inOrder(T->rchild);
}
}
Node *Insert(Node *T,int x)
{
if(T==NULL)
{
T=create();
T->c=x;
return T;
}else if(x<T->c)
{
T->lchild=Insert(T->lchild,x);
}else if(x>T->c)
{
T->rchild=Insert(T->rchild,x);
}
return T;
}
int main(){
int n;
char tmp[12];
while(scanf("%d",&n)!=EOF && n!=0)
{
loc=0;
Node *T=NULL;
scanf("%s",tmp);
for(int i=0;tmp[i]!=0;i++)
{
T=Insert(T,tmp[i]-'0');
}
size1=0;
str=str1;
size=&size1;
postOrder(T);
inOrder(T);
str1[size1]=0;
while(n--!=0)
{
scanf("%s",tmp);
Node *T2=NULL;
for(int i=0;tmp[i]!=0;i++)
{
T2=Insert(T2,tmp[i]-'0');
}
size2=0;
str=str2;
size=&size2;
postOrder(T2);
inOrder(T2);
str2[size2]=0;
puts(strcmp(str1,str2)==0?"YES":"NO");
}
}
return 0;
}