-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostorder Traversal.cpp
71 lines (68 loc) · 1.16 KB
/
Postorder Traversal.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
64
65
66
67
68
69
70
71
#include <iostream>
int a[50010];
int b[50010];
int n;
/*int build(int p, int q)
{
if (p + 1 == q)
{
std::cout << b[p];
return 0;
}
int temp;
for (int i = 0; i < n; i++)
{
int j;
for (j = p; j < q; j++)
{
if (b[j] == a[i])
{
temp = j;
break;
}
}
if (j < q)
break;
}
if (p < temp)
build(p, temp);
else
build(temp + 1, q);
return 0;
}*/
int main()
{
std::ios::sync_with_stdio(false);
std::cin >> n;
for (int i = 0; i < n; i++)
{
std::cin >> a[i];
}
for (int i = 0; i < n; i++)
{
std::cin >> b[i];
}
int p = 0, q = n;
int temp;
for (int i = 0; i < n; i++)
{
for (int j = p; j < q; j++)
{
if (b[j] == a[i])
{
temp = j;
break;
}
}
if (temp > p)
q = temp;
else if (temp + 1 < q)
p = temp + 1;
else
{
std::cout << b[temp];
break;
}
}
return 0;
}