ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-08-20 02:31:31  阅读:217  来源: 互联网

标签:Binary PAT int 30 father lcau ERROR found LCA


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;
}

标签:Binary,PAT,int,30,father,lcau,ERROR,found,LCA
来源: https://www.cnblogs.com/lingchen1642/p/15164750.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有