https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312

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 left 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 left 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 (≤ 1,000), the number of pairs of nodes to be tested; and N (≤10,000), 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.

代码:

#include <bits/stdc++.h>
using namespace std; int N, M;
vector<int> pre;
map<int, int> mp; int main() {
scanf("%d%d", &M, &N);
pre.resize(N);
for(int i = 0; i < N; i ++) {
scanf("%d", &pre[i]);
mp[pre[i]] = 1;
}
while(M --) {
int a, b;
scanf("%d%d", &a, &b);
if(!mp[a] && !mp[b])
printf("ERROR: %d and %d are not found.\n", a, b);
else if(!mp[a] || !mp[b])
printf("ERROR: %d is not found.\n", mp[a] ? b : a);
else {
int root;
for(int i = 0; i < N; i ++) {
root = pre[i];
if((root >= a && root <= b) || (root <= a && root >= b)) break;
} if(root == a)
printf("%d is an ancestor of %d.\n", a, b);
else if(root == b)
printf("%d is an ancestor of %d.\n", b, a);
else printf("LCA of %d and %d is %d.\n", a, b, root);
}
}
return 0;
}

  用 mp 记下是否出现过 然后只要从头找满足值在 a b 之间的就是根了

FHFHFH 过年之前最后一个工作日

PAT 甲级 1143 Lowest Common Ancestor的更多相关文章

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

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

  2. PAT Advanced 1143 Lowest Common Ancestor (30) [二叉查找树 LCA]

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

  3. 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 ...

  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

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

  6. 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 (30)

    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 (30 分)——二叉搜索树,lca

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

随机推荐

  1. 安装好XAMPP+安装好PhpStorm 然后搭建PHP开发环境

    1.安装XAMPP 1.1.可以参考我的这篇博客:XMAPP的安装与配置. 2.安装并破解PhpStorm 2.1.可以参考我的这篇博客:PhpStorm2016.2版本安装与破解. 3.配置PhpS ...

  2. 20155238 2016-2017-2 《JAVA程序设计》第七周学习总结

    教材学习内容总结 第十二章 认识Lambda语法 在只有Lambda表达式的情况下,参数的类型必须写出来. 匿名类有相应的应用场合.Lambda表达式只关心方法命名上的参数与返回定义,忽略方法名称. ...

  3. 2017-2018-1 20155317 《信息安全系统设计基础》课堂实践——实现mypwd

    2017-2018-1 20155317 <信息安全系统设计基础>课堂实践——实现mypwd 1 . 学习使用pwd 很显然pwd命令的意思是打印出该文件当前的绝对路径 2 . 了解pwd ...

  4. Linux部署python django程序-apache

    1.安装Apache 先卸载自带的httpd rpm -e httpd --nodeps 在网上下载四个文件 1.apr-1.4.6.tar.gz 2.apr-util-1.5.1.tar.gz 3. ...

  5. 15-[JavaScript]-ECMAScript 1

    0.javaScript的发展历程 https://zhuanlan.zhihu.com/p/27985124 1.javaScript是什么? javaScript是一种web前端的描述语言,也是一 ...

  6. flask 实现异步非阻塞----gevent

    我们都知道,flask不支持异步非阻塞的请求,我们可以创建一个新项目去测试一下,推荐大家使用pycharm去开发我们的flask  使用特别的方便. rom flask import Flask im ...

  7. 【日常训练】Help Chef Gerasim(Codeforces 99B)

    题意与分析 需要注意非法情况.换言之,合法情况其实很苛刻. 代码 /* * ACM Code => cf99b.java * Written by Sam X * Date: 三月, 19, 2 ...

  8. 高大上网站-CSS3总结1-图片2D处理以及BUG修复

    高大上网站-CSS3总结1-图片2D处理以及BUG修复 一,前言: 现在的前端UI相对JS来说,重视并不够. 但是CSS3提供的新特性,将现在的网站赤裸裸的划分为两类:一类还在写着老旧样式,或者通过b ...

  9. Jmeter+ant+jenkins接口自动化测试 平台搭建(二)

    一.依赖文件配置 1.在ant目录C:\apache-ant-1.10.5下新建一个demo文件夹,并将jmeter测试脚本放在该文件夹中 2.将\apache-jmeter-3.3\extras下面 ...

  10. Charles工具内存不足时解决办法

    Charles runs out of memory After recording for a while Charles will run low on available memory. To ...