-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpreempos.c
63 lines (50 loc) · 1.16 KB
/
preempos.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
// iagorr ;)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define right(i) (i*2+1)
#define left(i) (i*2)
const int max = 256*2;
char *solve(char *preorder, char *inorder, int n) {
char *tree = malloc(sizeof(char)*max);
for(int i = 0; i < max; ++i) tree[i] = 0;
int mappos[256*2];
for(int i = 0; i < n; ++i)
mappos[inorder[i]] = i;
tree[1] = preorder[0];
for(int i = 1; i < n; ++i){
char toinsert = preorder[i];
int toinsertpos = mappos[toinsert];
int node = 1;
while(tree[node]) {
int nodepos = mappos[tree[node]];
if(toinsertpos < nodepos){
node = left(node);
}
else {
node = right(node);
}
}
tree[node] = toinsert;
}
return tree;
}
void posorder(char *root, int i) {
if(i < max && root[i]) {
posorder(root, left(i));
posorder(root, right(i));
printf("%c", root[i]);
}
}
int main(void) {
int t; scanf("%d", &t);
while(t--) {
int n; scanf("%d", &n);
char a[53*2]; char b[53*2];
scanf("%s %s", a, b);
char *c = solve(a, b, n);
posorder(c, 1); printf("\n");
}
}
// AC, árvore binária de busca, travessia.