[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 题意: 给定一个二叉搜索树,以及他的前 ...
随机推荐
- C++中友元简介
本文基于<C++ Primer(第五版)>,纯属个人笔记整理.若有错误欢迎大家留言指出. 一.为什么要用友元和其缺点? 采用类的机制后实现了数据的隐藏与封装,类的数据成员一般定义为私有成员 ...
- 项目管理---git----快速使用git笔记(七)------coding.net项目管理多人操作的流程规范--合并代码审核
我们在前面已经介绍了coding.net和本地git的基本用法. 但是多人协作开发时情况会复杂得多,所以我们最好有一些规范来保证项目多人开发顺利进行. 比如说 规范一 master代码分支 需要开启 ...
- apply的理解和数组降维
func.apply(thisObj, [argArray] ); apply方法用来改变函数执行时的this指向,后面的参数是一个类数组对象,可以是数组,arguments,甚至一个有length属 ...
- node egg.js使用superagent做文件转发
使用 egg.js + superagent 进行文件上传转发 // app/controller/file.js const Controller = require('egg').Controll ...
- train val test区别
train是训练集,val是训练过程中的测试集,是为了让你在边训练边看到训练的结果,及时判断学习状态.test就是训练模型结束后,用于评价模型结果的测试集.只有train就可以训练,val不是必须的, ...
- CCPC-Winter Camp div2 day8 A
---恢复内容开始--- 题目: 题解: 我们考虑第i个叶子节点的情况,根据题目给的输入条件,我们可以知道,深度大的节点的序号一定是大于深度浅的节点的序号的 如图 题目要求我们找出每一个叶子节点距离编 ...
- 【题解】Weird journey Codeforces 788B 欧拉路
传送门:http://codeforces.com/contest/788/problem/B 好题!好题! 首先图不连通的时候肯定答案是0,我们下面讨论图联通的情况 首先考虑,如果我们每条边都经过两 ...
- 【STSRM10】数学上来先打表
[算法]DP+数学计数 [题意]给出n个点(不同点之间有区别),求出满足下列条件的连边(双向边)方案(对1004535809取模): 1.每条边连接两个不同的点,每两个点之间至多有一条边. 2.不存在 ...
- 【洛谷 P2042】 [NOI2005]维护数列(自闭记第一期)
题目链接 首先,这题我是没A的..太毒瘤了 题目本身不难,都是\(Splay\)的基操,但是细节真的容易挂. 调了好久自闭了,果断放弃.. 希望本节目停更. 放上最终版本 #include <c ...
- 阿里云服务器下安装配置 vsftpd —— 基于CentOS 6.3 【简洁版】
原文链接:http://www.tuicool.com/articles/nuiQBja 1.更新yum源 我是直接 yum update 更新的 2.安装vsftp 使用yum命令安装vsftpd ...