PAT 甲级 1143 Lowest Common Ancestor
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的更多相关文章
- PAT甲级1143 Lowest Common Ancestor【BST】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
随机推荐
- mypwd的实现——20155328
mypwd的实现 分析 pwd不带参数时,实现的是查看并打印当前所在位置的绝对路径功能. 如图: 所以实现mypwd时重点在于循环打印路径名,循环的终止条件是是到了根目录.判定是否到达根目录的标准为: ...
- mfc 类
知识点 类的概念 类的相关术语 定义类 使用类 一.类的概念 简单的说类就是数据与函数综合体,它是用户自定义类型. 二.类的相关术语 类的实例称为对象. 类在定义中隐式地包含数据和操作函数,这种思想称 ...
- tomcat : 虚拟路径映射
tomcat设置虚拟访问地址和真实路径的映射,方法有几种,这里介绍常用的两种方式: 一.修改server.xml文件: 步骤如下: 1.在tomcat根目录下打开conf文件夹,该文件夹下有个ser ...
- 【Menu】 目录索引
一. 开发语言(25%) 1.Python 2.shell 3.C.C++ 4.java 5.html 二.系统(25%) 1.redhat Linux 2.suse Linux 3.windows ...
- CF833B The Bakery 线段树,DP
CF833B The Bakery LG传送门 线段树优化DP. 其实这是很久以前就应该做了的一道题,由于颓废一直咕在那里,其实还是挺不错的一道题. 先考虑\(O(n^2k)\)做法:设\(f[i][ ...
- Nopcommerce主要用到的技术及特点
很多人都说通过阅读.学习大神们高质量的代码是提高自己技术能力最快的方式之一.我觉得通过阅读NopCommerce的源码,可以从中学习很多企业系统.软件开发的规范和一些新的技术.技巧,可以快速地提高我们 ...
- [原]Asp.net Core 2.1.2 测试成功Ajax上传文件新解法
利用layui框架可以上传文件调试拦截成功! [HttpPost] public IActionResult Method1(IFormFile file) { return Json(new{suc ...
- Effective C++(Third Edition) Item29 为“异常安全”而努力是值得的
“异常安全”有两个条件: 1.不泄露任何资源 可以通过以对象管理资源的方式(Item13). 2.不允许数据败坏 异常安全函数提供以下三种保证之一 a.基本承诺 如果异常被抛出,程序内的任何事物都仍然 ...
- 简单字典实现(KV问题)
搜索二叉树基本概念请看上篇博客 这两个问题都是典型的K(key)V(value)问题,我们用KV算法解决. 判断一个单词是否拼写正确:假设把所有单词都按照搜索树的性质插入到搜索二叉树中,我们判断一个单 ...
- weblogic在linux和window下的安装
weblogic在linux和window下的安装 weblogic下载地址 Windows server2008 一直下一步没什么坑 centos6.5 使用rpm安装jdk8 JDK下载 安装jd ...