题目

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.

A binary search tree (BST) is recursively defined as a binary tree which has the following properties:

  • The lef subtree of a node contains only nodes with keys less than the node’s key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node’s key.
  • Both the lef and right subtrees must also be binary search trees.

Given any two nodes in a BST, 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 (<= 1000), the number of pairs of nodes to be tested; and N (<= 10000), the number of keys in the BST, respectively. In the second line, N distinct integers are given as the preorder traversal sequence of the BST. 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 BST, 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

6 3 1 2 5 4 8 7

2 5

8 7

1 9

12 -3

0 8

99 99

Sample Output:

LCA of 2 and 5 is 3.

8 is an ancestor of 7.

ERROR: 9 is not found.

ERROR: 12 and -3 are not found.

ERROR: 0 is not found.

ERROR: 99 and 99 are not found.

题目分析

已知二叉查找树的前序序列,求每个测试样例中两个节点最近公共祖先节点

解题思路

利用二叉查找树特点:根节点,大于左子树所有结点,小于右子树所有结点

前序序列依次遍历,即为从左到右从下到上每个根节点,判断测试样例两个节点与每个根节点大小关系

  1. u,v在root两边,则root为u,v最近公共祖先节点
  2. u,v都在root左边,则最近公共祖先在root左子树,递归查找
  3. u,v都在root右边,则最近公共祖先在root右子树,递归查找
  4. u==root,则u是v的最近公共祖先节点
  5. v==root,则v是u的最近公共祖先节点

易错点

利用前序升序排序得到中序,前序+中序查找lca会有一个测试点超时(对比A1151)

Code

  1. #include <iostream>
  2. #include <vector>
  3. #include <map>
  4. using namespace std;
  5. map<int,bool> mp;
  6. int main(int argc,char * argv[]) {
  7. int m,n,u,v,a;
  8. scanf("%d %d",&m,&n);
  9. vector<int> pre(n);
  10. for(int i=0; i<n; i++) {
  11. scanf("%d",&pre[i]);
  12. mp[pre[i]]=true;
  13. }
  14. for(int i=0; i<m; i++) {
  15. scanf("%d %d",&u,&v);
  16. for(int i=0; i<n; i++) {
  17. a=pre[i];
  18. if(a>=u&&a<=v||a<=u&&a>=v)break;
  19. }
  20. if(mp[u]==false&&mp[v]==false) //都没找到
  21. printf("ERROR: %d and %d are not found.\n",u,v);
  22. else if(mp[u]==false||mp[v]==false)
  23. printf("ERROR: %d is not found.\n",mp[u]==false?u:v);
  24. else if(a==v||a==u)
  25. printf("%d is an ancestor of %d.\n",a,a==v?u:v);
  26. else
  27. printf("LCA of %d and %d is %d.\n",u,v,a);
  28. }
  29. return 0;
  30. }

PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]的更多相关文章

  1. PAT 甲级 1143 Lowest Common Ancestor

    https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...

  2. 1143. Lowest Common Ancestor (30)

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  3. PAT甲级1143 Lowest Common Ancestor【BST】

    题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...

  4. [PAT] 1143 Lowest Common Ancestor(30 分)

    1143 Lowest Common Ancestor(30 分)The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  5. PAT 1143 Lowest Common Ancestor[难][BST性质]

    1143 Lowest Common Ancestor(30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...

  6. PAT 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  7. 1143 Lowest Common Ancestor

    The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...

  8. [PAT] 1143 Lowest Common Ancestor(30 分)1145 Hashing - Average Search Time(25 分)

    1145 Hashing - Average Search Time(25 分)The task of this problem is simple: insert a sequence of dis ...

  9. PAT_A1143#Lowest Common Ancestor

    Source: PAT A1143 Lowest Common Ancestor (30 分) Description: The lowest common ancestor (LCA) of two ...

随机推荐

  1. P1061 判断题

    P1061 判断题 转跳点:

  2. js - 获取当前年月日

    var date = new Date(); date .getYear(); //获取当前年份(2位) date .getFullYear(); //获取完整的年份(4位) date .getMon ...

  3. JS写一个旋转木马的视频播放效果

    JS以及JQ的功能很强大,可以做出很多的优秀效果.今天给大家分享一个我之前写网站用到的旋转木马效果. 大概效果图就是这样的,上面的视频播放是旋转木马效果. 下面的音乐播放效果放在下一篇内容里面讲. 直 ...

  4. java随机函数用法Random

     原文地址:http://blog.csdn.net/wpjava/article/details/6004492  import java.util.Random; public class Ran ...

  5. 刷题21. Merge Two Sorted Lists

    一.题目说明 这个题目是21. Merge Two Sorted Lists,归并2个已排序的列表.难度是Easy! 二.我的解答 既然是简单的题目,应该一次搞定.确实1次就搞定了,但是性能太差: R ...

  6. java学习之IO流(学习之旅,一)

    个人在学习IO流的时候看到如下所示java 流类图结构的时候,我的感想是,这么多··处于蒙的状态. Java流类图结构 这么多,没有分类不好学,那我们就慢慢一口一口的吃,这样每天学习一点就好了,其实很 ...

  7. 提升Essay写作说服力,需要注意这几个细节

    很多留学生对于essay写作都不精通,能够勉强通过就不错了.那么Essay写作到底该怎么提分呢?可以从哪些方面入手?小编给同学们指几条路,相信可以帮到大家. 在有说服力的Essay中总结您的论点.尽管 ...

  8. ACM-Antiprime数

      问题描述: swust打不开,随便找了个博客.... 对于任何正整数x,起约数的个数记做g(x).例如g(1)=1,g(6)=4. 定义:如果某个正整数x满足:对于任意i(0<i<x) ...

  9. [BJDCTF2020]EasySearch

    0x00 知识点 Apache SSI 远程命令执行漏洞 链接: https://www.cnblogs.com/yuzly/p/11226439.html 当目标服务器开启了SSI与CGI支持,我们 ...

  10. Golang的类型转换实战案例

    Golang的类型转换实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型概述 基础数据类型概述,博主推荐阅读: 布尔型: https://www.cnblogs. ...