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 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 <iostream>
#include <vector>
#include <map>
using namespace std;
map<int, bool> mp;
int main() {
int m, n, u, v, a;
scanf("%d %d", &m, &n);
vector<int> pre(n);
for (int i = 0; i < n; i++) {
scanf("%d", &pre[i]);
mp[pre[i]] = true;
}
for (int i = 0; i < m; i++) {
scanf("%d %d", &u, &v);
for(int j = 0; j < n; j++) {
a = pre[j];
if ((a > u && a < v)|| (a > v && a < u) || (a == u) || (a == v)) break;
}
if (mp[u] == false && mp[v] == false)
printf("ERROR: %d and %d are not found.\n", u, v);
else if (mp[u] == false || mp[v] == false)
printf("ERROR: %d is not found.\n", mp[u] == false ? u : v);
else if (a == u || a == v)
printf("%d is an ancestor of %d.\n", a, a == u ? v : u);
else
printf("LCA of %d and %d is %d.\n", u, v, a);
}
return 0;
}
PAT 1143 Lowest Common Ancestor的更多相关文章
- 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(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 甲级 1143 Lowest Common Ancestor
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...
- 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 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 ...
- 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【BST】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...
随机推荐
- 使用Google Closure Compiler全力压缩代码(转)
JavaScript压缩代码的重要性不言而喻,如今的压缩工具也有不少,例如YUI Compressor,Google Closure Compiler,以及现在比较红火的UglifyJS.Uglify ...
- 深入浅出Android makefile(2)--LOCAL_PATH(转载)
转自:http://nfer-zhuang.iteye.com/blog/1752387 一.说明 上文我们对acp的Android.mk文件做了一个大致的描述,使得大家对Android.mk文件有了 ...
- 如何成为一名牛逼的C/C++程序员?
每一个学技术的人 都想干个三五年就能成为大牛 跳槽去大厂,薪水翻番 或者在一个小公司里,带个小团队 在30岁左右成为项目经理 晋升管理层 买房买车,实现人生理想 然而技术之路漫漫 想要成为牛×的程序员 ...
- (DP)51NOD 1118 机器人走方格
M * N的方格,一个机器人从左上走到右下,只能向右或向下走.有多少种不同的走法?由于方法数量可能很大,只需要输出Mod 10^9 + 7的结果. Input 第1行,2个数M,N,中间用空格隔开.( ...
- H5活动的一些事
ISUX团队镇楼:https://isux.tencent.com/nine-question-of-swipe-html5-page.html IE6.7.8支持html5新元素 : http:// ...
- 横向移动-广告图(web)
项目 (移动的广告牌) 要求: 1,实现图片一次以移动的方式出现,到最后一张完全出现时,回弹到第一张 2,鼠标放在图片上面图片移动,鼠标离开,图片停止移动 HTML结构 <!DOCTYPE ht ...
- ftp获取mysql数据库方法
我说的这种情况是针对mysql数据库的,首先下载一个mysql通过ftp放到站点里面,然后通过配置文件找到数据库的名字和密码,然后通过浏览器访问数据库,直接在域名后面加上下载的mysql文件的名字就可 ...
- poj2367 Genealogical tree
思路: 拓扑排序,这里是用染色的dfs实现的.在有环的情况下可以判断出来,没有环的情况下输出拓扑排序序列. 实现: #include <vector> #include <cstri ...
- Android基础TOP7_1:ListView制作列表
结构: Activity: activity_main: <RelativeLayout xmlns:android="http://schemas.android.com/apk/r ...
- oracle 手动配置服务器端和客户端
1.oracle 服务器端配置 将oracle安装完成之后,在Net Configuration Assistant配置 1.监听程序配置 先找到Net Configuration Assistant ...