-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path트리_4.c
92 lines (69 loc) · 1.42 KB
/
트리_4.c
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
#include <stdio.h>
#include <stdlib.h>
#pragma warning (disable : 4996)
typedef struct TreeNode {
int data;
struct TreeNode* left, * right;
}TreeNode;
TreeNode* findNode(TreeNode* root, int d) {
TreeNode* temp = root;
if (root == NULL)
return NULL;
if (root->data == d)
return root;
temp = findNode(root->left, d);
if (temp != NULL)
return temp;
return findNode(root->right, d);
}
TreeNode* makeNode(int d) {
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode));
node->data = d;
node->left = NULL;
node->right = NULL;
return node;
}
void connectNode(TreeNode* root, int d, int l, int r) {
TreeNode* p = findNode(root, d);
if (l != 0)
p->left = makeNode(l);
if (r != 0)
p->right = makeNode(r);
}
TreeNode* makeTree() {
int n, d, l, r;
TreeNode* root;
scanf("%d", &n);
scanf("%d %d %d", &d, &l, &r);
root = makeNode(d);
root->left = makeNode(l);
root->right = makeNode(r);
for (int i = 1; i < n; i++) {
scanf("%d %d %d", &d, &l, &r);
connectNode(root, d, l, r);
}
return root;
}
void traversal(TreeNode* root, char* str) {
printf(" %d", root->data);
if (*str) {
if (*str == 'R')
traversal(root->right, str + 1);
else if (*str == 'L')
traversal(root->left, str + 1);
}
}
int main() {
TreeNode* root = makeTree();
int n;
scanf("%d", &n);
getchar();
char str[10];
for (int i = 0; i < n; i++) {
scanf("%s", str);
getchar();
traversal(root, str);
printf("\n");
}
return 0;
}