-
Notifications
You must be signed in to change notification settings - Fork 0
/
1009BinaryTreeEqual.cpp
141 lines (138 loc) · 2.68 KB
/
1009BinaryTreeEqual.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#include<iostream>
#include<string>
#include<vector>
using namespace std;
class Node
{
public:
int data;
Node *lchild;
Node *rchild;
Node(int da=0):data(da)
{
lchild=rchild=0;
}
Node(int da,Node *l,Node *r)
{
data=da;
lchild=l;
rchild=r;
}
};
class BinSearTree
{
public:
Node *root;
void clear(Node *root)
{
if(root!=NULL)
{
clear(root->lchild);
clear(root->rchild);
root->lchild=NULL;
root->rchild=NULL;
delete root;
root=NULL;
}
}
BinSearTree()
{
root=0;
}
BinSearTree(int da)
{
root=new Node(da);
}
void InsertNode(int da)
{
Node *p=root,*pre=NULL;
while(p)
{
pre=p;
if(da<(p->data))
p=p->lchild;
else
p=p->rchild;
}
if(root==NULL)
root=new Node(da);
else if(da<(pre->data))
pre->lchild=new Node(da);
else
pre->rchild=new Node(da);
}
int visit(Node *n)
{
return n->data;
}
string Inorder(Node *root)
{
string str;
if(root!=NULL)
{
str+=Inorder(root->lchild);
str+=visit(root)+'0';
str+=Inorder(root->rchild);
}
return str;
}
string Preorder(Node *root)
{
string str;
if(root!=NULL)
{
str+=visit(root)+'0';
str+=Preorder(root->lchild);
str+=Preorder(root->rchild);
}
return str;
}
Node *getRoot()
{
return root;
}
};
int main()
{
vector<string> ve;
vector<string> ve1;
BinSearTree bt;
string num;//要输入的数
int n;//输入几组数与原来的比较
char c;
while(cin>>n&&n!=0)
{
cin.get(c);
ve.resize(n+1);
ve1.resize(n+1);
getline(cin,num);
for(int i=0; i<num.size(); i++)
{
bt.InsertNode(num[i]-'0');
}
Node *no=bt.getRoot();
ve[0]=bt.Inorder(no);
ve1[0]=bt.Preorder(no);
for(int j=1; j<=n; j++)
{
getline(cin,ve[j]);
BinSearTree bt1;
for(int i=0; i<ve[j].size(); i++)
{
bt1.InsertNode(ve[j][i]-'0');
}
no=bt1.getRoot();
ve[j]=bt1.Inorder(no);
ve1[j]=bt1.Preorder(no);
}
}
for(int i=1; i<ve.size(); i++)
{
if(ve[i]==ve[0]&&ve1[i]==ve1[0])
cout<<"YES";
else
cout<<"NO";
if(i<ve.size()-1)
cout<<endl;
}
}