1151 LCA in a Binary Tree (30point(s)) - C语言 PAT 甲级

1151 LCA in a Binary Tree (30point(s))

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.

Input Specification:

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.

Output Specification:

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…

Sample Input:

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

Sample Output:

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 个节点的二叉树,并给出二叉树的先序序列和中序序列

每次查询给两节点,求这两个节点的最低公共祖先

设计思路:

倍增法

  1. 利用先序和中序序列,dfs 遍历建立倍增法需要的信息
  2. 对于每次查询,用倍增法查询输出结果

理解倍增法,先看看倍增法的原始退化版,一倍倍增法:

  • 对于每个节点用deep[] 记录节点深度,dp[] 数组记录自己的父节点
  • 对于需要查询的两个节点,把深度大的节点利用 dp[] 记录的父节点,一步一步跳到深度小的节点的同一深度
    • 深度大的节点移动完后,若恰巧移动到了深度小的节点上,说明深度小的节点就是这两节点的祖先
    • 若不是,说明这两节点的祖先一定在深度更小的上方,因为此时俩节点深度相同,顺着 dp[] 俩节点同时一步一步跳,直到跳到同一节点,此节点即为俩节点的最低公共节点

节点很多,一步一步跳很容易超时,所以在此基础上,产生了真正的倍增法:

  • dp[][] 的第二维记录每个节点跳 2j 层到达的父节点
    • dp[][0] 记录跳 1 = 20 层的父节点
    • dp[][1] 记录跳 2 = 21 层的父节点
    • dp[][2] 记录跳 4 = 22 层的父节点
    • ……
    • dp[][31] 记录跳 231 层的父节点,如果树的节点深度够深,有那么远的父节点的话
  • 与一倍跳法不同,倍增法,优先大跳,首先往最远跳,慢慢减小跳的距离,最终到达需要的节点,因为任意整数,一定等于若干 2j 相加
  • 而且可以看出 INT 大小的树,极端情况退化成链表也只需跳 32 下就到根节点

Reference:

编译器:C (gcc)
#include <stdio.h>

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值