-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQUADTREE_OPTIMIZED.cpp
63 lines (51 loc) · 1.18 KB
/
QUADTREE_OPTIMIZED.cpp
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
#include <stdio.h>
#include <string.h>
int preorder[100];
int inorder[100];
int pos = 0;
int memo[1000 + 1];
void printPost(int lower, int upper);
void reset();
int main()
{
int nTest = 0;
scanf("%d", &nTest);
while (nTest-- > 0)
{
scanf("%d", &nNode);
int i = 0, j = 0;
while (i < nNode)
{
scanf("%d", &preorder[i]);
i++;
}
while (j < nNode)
{
scanf("%d", &inorder[j]);
memo[ inorder[j] ] = j;
j++;
}
printPost(0, nNode);
printf("\n");
reset();
}
}
void printPost(int lower, int upper)
{
if (lower > upper) return;
if (lower >= nNode) return;
if (upper < 0) return;
int root = preorder[pos++]; // 9
int inorderIndex = memo[ root ]; // 3 -> 2 -> 0 -> 1 -> 5 -> 4 -> 6 // 0
if (inorderIndex == lower) lower++;
if (inorderIndex == upper) upper--;
printPost(lower, inorderIndex);
printPost(inorderIndex + 1, upper);
printf("%d ", root);
}
void reset()
{
nNode = 0;
pos = 0;
memset(used, 0, sizeof(used));
}