[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 题意: 给定一个二叉搜索树,以及他的前 ...
随机推荐
- [BZOJ3523][Poi2014]KLO-Bricks——全网唯一 一篇O(n)题解+bzoj最优解
Description 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合 ...
- IDEA的使用总结篇-1
随笔:随着回首所在的公司的日益扩大,所在的技术中心也日渐兵强马壮,但由于各位新老同仁的开发工具一直未曾统一,所以小编的老大终于一声令下,统一开发工具!统一使用IDEA,而且每个人今天要交一份IDEA的 ...
- [codeforces/edu30]总结(E)
链接:http://codeforces.com/contest/873/ A题: 贪心,把最大的k个数变成x即可. B题: 从左向右枚举右端点,维护balance的最长长度.任意一个子串可以看做两个 ...
- 007.C++构造函数
1.一个引例 //class head class complex //class body {} { public: complex(double r=0, double i) :re(r), im ...
- [Spark经验一]Spark RDD计算使用的函数里尽量不要使用全局变量
比如RDD里的计算调用了别的组件类里的方法(比如hbase里的put方法),那么序列化时,会将该方法所属的对象的所有变量都序列化的,可能有些根本没有实现序列化导致直接报错.也就是spark的api没有 ...
- redis安装----非基于lnmp安装
在 Ubuntu 系统安装 Redi 可以使用以下命令: $sudo apt-get update $sudo apt-get install redis-server 启动 Redis $ redi ...
- mpvue开发小记
1.组件嵌套组件时,子组件作用域bug 组件A内的slot包含子组件B的话,无法正常使用变量(这种情况下,B组件的template错误地使用了A的作用域). 我的解决方案:减少一层组件提炼,即这种情况 ...
- 南阳ACM 题目811:变态最大值 Java版
变态最大值 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 Yougth讲课的时候考察了一下求三个数最大值这个问题,没想到大家掌握的这么烂,幸好在他的帮助下大家算是解决了 ...
- [洛谷P1822] 魔法指纹
洛谷题目连接:魔法指纹 题目描述 对于任意一个至少两位的正整数n,按如下方式定义magic(n):将n按十进制顺序写下来,依次对相邻两个数写下差的绝对值.这样,得到了一个新数,去掉前导0,则定义为ma ...
- bzoj 2762: [JLOI2011]不等式组——树状数组
旺汪与旺喵最近在做一些不等式的练习.这些不等式都是形如ax+b>c 的一元不等式.当然,解这些不等式对旺汪来说太简单了,所以旺喵想挑战旺汪.旺喵给出一组一元不等式,并给出一个数值 .旺汪需要回答 ...