oj 二叉树相关

数据结构实验之二叉树一:树的同构
在没有new的时候,不会自动调用析构函数

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node{
 4     char u;
 5     int left;
 6     int right;
 7     Node(){
 8         u = 'A';
 9         left = 11;
10         right = 11;
11     }
12 }node1[20 + 3], node2[20 + 3];
13 
14 bool cal(int i, int j){
15     if(node1[node1[i].left].u == node2[node2[j].left].u && node1[node1[i].right].u == node2[node2[j].right].u)return true;
16     if(node1[node1[i].left].u == node2[node2[j].right].u && node1[node1[i].right].u == node2[node2[j].left].u)return true;
17     return false;
18 }
19 bool judge(int n, int m){
20     int i, j;
21     for(i = 0; i < n; i++){
22         for(j = 0; j < m; j++){
23             if(node1[i].u == node2[j].u){
24                 if(cal(i, j)){
25                     break;
26                 }
27                 else {
28                     return false;
29                 }
30             }
31         }
32         if(j == m){
33             return false;
34         }
35     }
36     return true;
37 }
38 int main(){
39     int n, m;
40     while(~scanf("%d", &n)){
41         char str[10];
42         int l, r, i;
43 
44         for(i = 0; i < n; i++){
45             scanf("%s", str);
46                 node1[i].u = str[0];
47             scanf("%s", str);
48             if(str[0] != '-'){
49                 node1[i].left = str[0] - '0';
50             }
51             else node1[i].left = 11;
52             scanf("%s", str);
53             if(str[0] != '-'){
54                 node1[i].right = str[0] - '0';
55             }
56             else 
57                 node1[i].right = 11;
58         }
59 
60         scanf("%d", &m);
61         for(i = 0; i < m; i++){
62             scanf("%s", str);
63                 node2[i].u = str[0];
64             scanf("%s", str);
65             if(str[0] != '-'){
66                 node2[i].left = str[0] - '0';
67             }
68             else node2[i].left = 11;
69             scanf("%s", str);
70             if(str[0] != '-'){
71                 node2[i].right = str[0] - '0';
72             }
73             else node2[i].right = 11;
74         }
75         if(judge(n, m))
76             printf("Yes\n");
77         else
78             printf("No\n");
79     }
80     return 0;
81 }

数据结构实验之二叉树二:遍历二叉树

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 struct Node
 4 {
 5     char u;
 6     Node* left;
 7     Node* right;
 8     Node()
 9     {
10         left = NULL;
11         right = NULL;
12     }
13 };
14 char str[100];
15 int i;
16 Node *create(int len)
17 {
18     if(i >= len)
19         return NULL;
20     if(str[i] == ',')
21     {
22         i++;
23         return NULL;
24     }
25     Node* tmp = new Node();
26     tmp->u = str[i++];
27     tmp->left = create(len);
28     tmp->right = create(len);
29     return tmp;
30 }
31 void ceng(Node* root)
32 {
33     if(root)
34     {
35         ceng(root->left);
36         printf("%c", root->u);
37         ceng(root->right);
38     }
39 }
40 void hou(Node* root)
41 {
42     if(root)
43     {
44         hou(root->left);
45         hou(root->right);
46         printf("%c", root->u);
47     }
48 }
49 int main()
50 {
51 
52     while(scanf("%s", str) != EOF)
53     {
54         i = 0;
55         Node* root = new Node();
56         int len = strlen(str);
57         root = create(len);
58         ceng(root);
59         printf("\n");
60         hou(root);
61         printf("\n");
62     }
63     return 0;
64 }
65 
66 
67 /***************************************************
68 User name: ACM2017002信科1701黄庆祥
69 Result: Accepted
70 Take time: 0ms
71 Take Memory: 200KB
72 Submit time: 2019-09-27 21:27:21
73 ****************************************************/

猜你喜欢

转载自www.cnblogs.com/letlifestop/p/11601455.html