PAT (Advanced Level) Practice 1151 LCA in a Binary Tree (30 分) 凌宸1642

PAT (Advanced Level) Practice 1151 LCA in a Binary Tree (30 分) 凌宸1642

题目描述:

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.

译:树中两个节点 U 和 V 的最低共同祖先 (LCA) 是将 U 和 V 作为后代的最深节点。

给定二叉树中的任意两个节点,您应该找到它们的 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.

译:每个输入文件包含一个测试用例。 对于每种情况,第一行给出两个正整数: M (≤ 1,000),要测试的节点对数; 和 N (≤ 10,000),分别是二叉树中的键数。 在以下两行中的每一行中,分别给出 N 个不同的整数作为二叉树的中序和前序遍历序列。 保证二叉树可以由输入序列唯一确定。 然后是 M 行,每行包含一对整数键 U 和 V。所有键都在 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 BST, print in a line ERROR: U is not found. or ERROR: V is not found. or ERROR: U and V are not found..

译:对于每对给定的 U 和 V,如果找到 LCA 并且 A 是密钥,则在一行中打印 LCA of U and V is A. 。 但是如果 A 是 U 和 V 之一,则打印 X is an ancestor of Y. 。其中 X 是 A,Y 是另一个节点。 如果在 BST 中未找到 U 或 V,则在一行中打印 ERROR: U is not found.ERROR: V is not found.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.

The Idea:

  • 1143 Lowest Common Ancestor (30 分) 很像,但此题没有 BST 加持,而是任意的二叉树。
  • 首先我选用的是静态建树,因为给了先序 + 中序 ,重建二叉树水到渠成。然后沿用 1143 题里的思路。先设计一个递归函数 search(root , u , v) ,然后再里面在调用另一个find(root , x)函数,但是这种过度使用 DFS ,显然最后两个测试点是超时的!!!
  • 然后借鉴了网上大佬的思路,重写了一个记录每个结点的层数和父节点的值即可找到 LCA:
    • 先存好每个结点的父节点和层号的映射(用map可以做到,然后变式利用 先序 + 中序重建二叉树可以做到)
    • 然后就是判断,判能不能找到这个简单,只要没有该结点的父节点映射,则说明是找不到的
    • u 和 v 都在树中时,先找到 u 和 v 处于同一层的祖先,也就是将 u和 v 中层号大(更深)的那一层先找到其与 层号小(更浅)的那一层的结点同一层的祖先结点 ;此时二者位于同一层中,在根据其相等与否,决定是否继续往上查找。二者相等时即找到了 LCA 。

The Codes:

#include <bits/stdc++.h>
using namespace std;
vector<int> in, pre;
unordered_map<int, int> father;
unordered_map<int, int> layer;
int m, n , root;

void init_father(int p, int left, int right, int l){
    int mid = left;
    while(pre[root] != in[mid]) mid++;
    father[pre[root]] = p;
    layer[pre[root]] = l;
    root++;
    if(mid-left > 0)init_father(in[mid] , left, mid - 1, l + 1) ;
    if(right-mid > 0)init_father(in[mid] , mid + 1, right , l + 1) ; 
}

int main(){
    scanf("%d%d", &m, &n);
    in.resize(n), pre.resize(n);
    for(int i=0; i<n; i++) scanf("%d", &in[i]);
    for(int i=0; i<n; i++) scanf("%d", &pre[i]);
    root = 0;
    init_father(pre[0], 0, n-1, 1);
    int u, v;
    while(m --){
        scanf("%d%d", &u, &v);
        if(father.find(u) == father.end() && father.find(v) == father.end())
            printf("ERROR: %d and %d are not found.\n", u, v);
        else if(father.find(u) == father.end())
            printf("ERROR: %d is not found.\n", u);
        else if(father.find(v) == father.end())
            printf("ERROR: %d is not found.\n", v);
        else{
            int lcau = u, lcav = v;
            while(layer[lcau]-layer[lcav] > 0) lcau = father[lcau];
            while(layer[lcav]-layer[lcau] > 0) lcav = father[lcav];
            while(lcau != lcav) lcau = father[lcau], lcav = father[lcav];
            if(lcau == u) printf("%d is an ancestor of %d.\n", u, v);
            else if(lcau == v) printf("%d is an ancestor of %d.\n", v, u);
            else printf("LCA of %d and %d is %d.\n", u, v, lcau);
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lingchen0522

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值