The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants.
Given any two nodes in a binary tree, you are supposed to find their LCA.
Each input file contains one test case. For each case, the first line gives two positive integers: M (≤ 1,000), the number of pairs of nodes to be tested; and N (≤ 10,000), the number of keys in the binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. Then M lines follow, each contains a pair of integer keys U and V. All the keys are in the range of int.
For each given pair of U and V, print in a line LCA of U and V is A. if the LCA is found and A is the key. But if A is one of U and V, print X is an ancestor of Y. where X is A and Y is the other node. If U or V is not found in the binary tree, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found…
6 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99
LCA of 2 and 6 is 3.
8 is an ancestor of 1.
ERROR: 9 is not found.
ERROR: 12 and -3 are not found.
ERROR: 0 is not found.
ERROR: 99 and 99 are not found.
输入 M 次查询,N 个节点的二叉树,并给出二叉树的先序序列和中序序列
每次查询给两节点,求这两个节点的最低公共祖先
倍增法
理解倍增法,先看看倍增法的原始退化版,一倍倍增法:
节点很多,一步一步跳很容易超时,所以在此基础上,产生了真正的倍增法:
Reference:
#include
int pre[10010], in[10010];
int deep[10010], dp[10010][32];
int get_step(int x)
{
int a, i;
for (a = 1, i = 0; (a <<= 1) <= x; i++)
;
return i;
}
void lca(int iu, int iv)
{
int temp, i;
int flag = 1;
int u = in[iu], v = in[iv];
if (deep[iu] < deep[iv]) {
temp = iu;
iu = iv;
iv = temp;
flag = 0;
}
for (i = get_step(deep[iu] - deep[iv]); i >= 0; i--)
if (deep[iu] - (1 << i) >= deep[iv])
iu = dp[iu][i];
if (iu == iv) {
printf("%d is an ancestor of %d.\n", in[iu], flag ? u : v);
} else {
for (i = get_step(deep[iu]); i >= 0; i--) {
// bu neng 32
if (dp[iu][i] != dp[iv][i]) {
iu = dp[iu][i];
iv = dp[iv][i];
}
}
printf("LCA of %d and %d is %d.\n", u, v, in[dp[iu][0]]);
}
return ;
}
void dfs(int iroot, int left, int right, int ifather, int d)
{
if (left > right)
return ;
int i = left, j;
while (i <= right && in[i] != pre[iroot])
i++;
deep[i] = deep[ifather] + 1;
dp[i][0] = ifather;
for (j = 1; (1 << j) <= deep[i]; j++)
dp[i][j] = dp[dp[i][j - 1]][j - 1];
dfs(iroot + 1, left, i - 1, i, d + 1);
dfs(iroot + 1 + i - left, i + 1, right, i, d + 1);
}
int main(void)
{
int n, m, u, v;
int i, j;
int iu, iv;
scanf("%d%d", &m, &n);
for (i = 1; i <= n; i++)
scanf("%d", &in[i]);
for (i = 1; i <= n; i++)
scanf("%d", &pre[i]);
dfs(1, 1, n, 0, 1);
for (i = 0; i < m; i++) {
scanf("%d%d", &u, &v);
iu = 0;
iv = 0;
for (j = 1; j <= n; j++) {
if (in[j] == u)
iu = j;
if (in[j] == v)
iv = j;
}
if ((iu == 0) && (iv == 0)) {
printf("ERROR: %d and %d are not found.\n", u, v);
} else if (iu == 0) {
printf("ERROR: %d is not found.\n", u);
} else if (iv == 0) {
printf("ERROR: %d is not found.\n", v);
} else {
lca(iu, iv);
}
}
return 0;
}