题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5058
强行二叉排序树(0ms)(stl set可做(31ms))
1 #pragma warning(disable:4996) 2 3 #include <algorithm> 4 #include <iostream> 5 #include <iomanip> 6 #include <cstring> 7 #include <climits> 8 #include <complex> 9 #include <fstream> 10 #include <cassert> 11 #include <cstdio> 12 #include <bitset> 13 #include <vector> 14 #include <deque> 15 #include <queue> 16 #include <stack> 17 #include <ctime> 18 #include <set> 19 #include <map> 20 #include <cmath> 21 22 using namespace std; 23 24 typedef struct Node { 25 Node* left; 26 Node* right; 27 int data; 28 Node() { left = NULL; right = NULL; } 29 }Node; 30 31 const int maxn = 110; 32 Node memory[maxn << 1]; 33 int CNT; 34 int n; 35 int tmp; 36 int cnt; 37 int a[maxn], b[maxn]; 38 39 Node* insert(Node* cur, int data) { 40 if (cur == NULL) { 41 cur = &memory[CNT++]; 42 cur->data = data; 43 return cur; 44 } 45 if (data > cur->data) { 46 cur->right = insert(cur->right, data); 47 } 48 if(data < cur->data){ 49 cur->left = insert(cur->left, data); 50 } 51 return cur; 52 } 53 54 void inorder(int* x, Node* root) { 55 if (root) { 56 inorder(x, root->left); 57 x[cnt++] = root->data; 58 inorder(x, root->right); 59 } 60 } 61 62 int main() { 63 //freopen("input", "r", stdin); 64 while (~scanf("%d", &n)) { 65 CNT = 0; 66 memset(a, 0, sizeof(a)); 67 memset(b, 0, sizeof(b)); 68 memset(memory, 0, sizeof(memory)); 69 Node* root1 = NULL; 70 for (int i = 0; i < n; i++) { 71 scanf("%d", &tmp); 72 root1 = insert(root1, tmp); 73 } 74 Node* root2 = NULL; 75 for (int i = 0; i < n; i++) { 76 scanf("%d", &tmp); 77 root2 = insert(root2, tmp); 78 } 79 cnt = 0; 80 inorder(a, root1); 81 cnt = 0; 82 inorder(b, root2); 83 int flag = 0; 84 for (int i = 0; i < n; i++) { 85 if (a[i] != b[i]) { 86 flag = 1; 87 break; 88 } 89 } 90 if (flag) printf("NO "); 91 else printf("YES "); 92 } 93 return 0; 94 }