-
Notifications
You must be signed in to change notification settings - Fork 96
/
10.1.cpp
67 lines (61 loc) · 1.83 KB
/
10.1.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
/*
* 题目名称:二叉搜索树
* 题目来源:浙江大学复试上机题
* 题目链接:http://t.cn/Ai9PUJtK
* 代码作者:杨泽邦(炉灰)
*/
#include <iostream>
#include <cstdio>
using namespace std;
struct TreeNode {
int data;
TreeNode* leftChild;
TreeNode* rightChild;
TreeNode(int x): data(x), leftChild(nullptr), rightChild(nullptr) {}
};
TreeNode* Insert(TreeNode* root, int x) {
if (root == nullptr) { //创建新节点
root = new TreeNode(x);
} else if (x < root->data) { //插入左子树
root->leftChild = Insert(root->leftChild, x);
} else if (root->data < x) { //插入右子树
root->rightChild = Insert(root->rightChild, x);
}
return root;
}
bool Same(TreeNode* original, TreeNode* root) {
if (original == nullptr && root == nullptr) {
return true;
}
if (original != nullptr && root != nullptr && original->data == root->data) {
return Same(original->leftChild, root->leftChild) && Same(original->rightChild, root->rightChild);
}
return false;
}
int main() {
int n;
while (scanf("%d", &n) != EOF) {
if (n == 0) {
break;
}
TreeNode* original = nullptr; //建立空树
string str;
cin >> str;
for (int j = 0; j < str.size(); ++j) {
original = Insert(original, str[j] - '0');
}
for (int i = 0; i < n; ++i) {
TreeNode* root = nullptr;
cin >> str;
for (int j = 0; j < str.size(); ++j) {
root = Insert(root, str[j] - '0');
}
if (Same(original, root)) {
printf("YES\n");
} else {
printf("NO\n");
}
}
}
return 0;
}