[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 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.
题意:寻找二分查找树中两个节点的最深公共祖先。
思路:
1.先通过先序遍历和中序遍历得到树。
2.判断两个结点是否在树中。
3.求得两个结点的父节点。
4.对两个父节点最深公共父节点。
柳婼 の blog
1.遍历这个前序遍历
2.如果这两个节点分别在当前节点的左右字数,或者当前节点等于这两个节点中的一个,则得到最深公共祖先。
题解:
#include<cstdio>
#include<vector>
#include<map>
using namespace std;
int main() {
int m, n;
map<int, bool> mp;
scanf("%d %d", &m, &n);
vector<int> pre(n);
; i<n; i++){
scanf("%d", &pre[i]);
mp[pre[i]] = true;
}
int u, v;
; i < m; i++) {
scanf("%d %d", &u, &v);
int a;
; j < pre.size(); j++) {
a = pre[j];
if ((a < u && a > v) || (a > u && a < v) || (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);
}
}
;
}
[PAT] 1143 Lowest Common Ancestor(30 分)的更多相关文章
- 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 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 ...
- 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
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 ...
- PAT 甲级 1143 Lowest Common Ancestor
https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 The lowest common ance ...
- 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 ...
- PAT甲级1143 Lowest Common Ancestor【BST】
题目:https://pintia.cn/problem-sets/994805342720868352/problems/994805343727501312 题意: 给定一个二叉搜索树,以及他的前 ...
随机推荐
- HDU4305:Lightning(生成树计数+判断点是否在线段上)
Lightning Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- bzoj 1811: [Ioi2005]mea 贪心,乱搞
[Ioi2005]mea Time Limit: 20 Sec Memory Limit: 64 MBSubmit: 690 Solved: 257[Submit][Status][Discuss ...
- C11简洁之道:模板改进
1. 右尖括号 我们在C++98/03中使用泛型编程的时候,经常遇到“>>”被当作右移操作符,而不是模板参数的结尾.假如我们有如下代码: template <typename T& ...
- 【poj1901-求区间第k大值(带修改)】树状数组套主席树
901: Zju2112 Dynamic Rankings Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 7025 Solved: 2925[Sub ...
- 【BZOJ1038】【ZJOI2008】瞭望塔 [模拟退火]
瞭望塔 Time Limit: 10 Sec Memory Limit: 162 MB[Submit][Status][Discuss] Description 致力于建设全国示范和谐小村庄的H村村 ...
- 【BZOJ4069】【APIO2015】巴厘岛的雕塑 [贪心][DP]
巴厘岛的雕塑 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 印尼巴厘岛的公路上有许多的雕塑, ...
- 51nod 1190 最小公倍数之和 V2
给出2个数a, b,求LCM(a,b) + LCM(a+1,b) + .. + LCM(b,b). 例如:a = 1, b = 6,1,2,3,4,5,6 同6的最小公倍数分别为6,6,6,12,30 ...
- 【BZOJ】1709: [Usaco2007 Oct]Super Paintball超级弹珠
[算法]模拟 [题解]O(n^2)预处理横线(y),纵线(x),主对角线(y-x+n),副对角线(x+y). 然后n^2枚举每个点.
- 【洛谷 P4320】 道路相遇 (圆方树,LCA)
题目链接 题意:给一张无向图和\(M\)个询问,问\(u,v\)之间的路径的必经之点的个数. 对图建出圆方树,然后必经之点就是两点路径经过的原点个数,用\((dep[u]+dep[v]-dep[LCA ...
- 多重部分和问题 (dp)
题目描述 有n种不同大小的数字Ai,每种各Mi个.判断是否能从这些数字中选出若干个使它们的和恰好为K. 这个问题可以用DP求解,递推关系式的定义会影响最终的复杂度. 第一种定义: dp[i+1][j] ...