[HDOJ5058]So easy

本博客介绍了一个关于强行二叉排序树的问题,包括如何插入节点、中序遍历获取排序序列以及通过比较两个排序序列来判断两棵强行二叉排序树是否相同。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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\n");
91         else        printf("YES\n");
92     }
93     return 0;
94 }

 

转载于:https://www.cnblogs.com/kirai/p/4911545.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值