-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLCA in a Binary Tree.cpp
71 lines (66 loc) · 1.6 KB
/
LCA in a Binary Tree.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 <algorithm>
#include <iostream>
#include <map>
std::map<int, int> s;
int pre[10010], in[10010];
int n, m;
int find(int p, int q, int p1, int p2, int root)
{
int inp = s[pre[root]] - 1;
if ((inp - p1) * (inp - p2) < 0)
{
std::cout << "LCA of " << in[p1] << " and " << in[p2] << " is "
<< in[inp] << "." << std::endl;
return 0;
}
if (inp == p1)
{
std::cout << in[p1] << " is an ancestor of " << in[p2] << "." << std::endl;
return 0;
}
if (inp == p2)
{
std::cout << in[p2] << " is an ancestor of " << in[p1] << "." << std::endl;
return 0;
}
if (p1 < inp && p2 < inp)
{
find(p, inp, p1, p2, root + 1);
}
else
{
find(inp + 1, q, p1, p2, root + inp - p + 1);
}
return 0;
}
int main()
{
std::ios::sync_with_stdio(false);
std::cin >> m >> n;
for (int i = 0; i < n; i++)
{
std::cin >> in[i];
s[in[i]] = i + 1;
}
for (int i = 0; i < n; i++)
{
std::cin >> pre[i];
}
for (int i = 0; i < m; i++)
{
int x, y;
std::cin >> x >> y;
int p1 = s[x] - 1, p2 = s[y] - 1;
if (p1 == -1 && p2 == -1)
std::cout << "ERROR: " << x << " and " << y << " are not found." << std::endl;
else if (p1 == -1 && p2 != -1)
std::cout << "ERROR: " << x << " is not found." << std::endl;
else if (p2 == -1 && p1 != -1)
std::cout << "ERROR: " << y << " is not found." << std::endl;
else
{
find(0, n, p1, p2, 0);
}
}
return 0;
}