当前位置:网站首页>PAT甲级1151 LCA in a Binary Tree (30 分)
PAT甲级1151 LCA in a Binary Tree (30 分)
2022-07-25 15:24:00 【nekoha_dexter】
1151 LCA in a Binary Tree (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.
Given any two nodes in a binary tree, 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 binary tree, respectively. In each of the following two lines, N distinct integers are given as the inorder and preorder traversal sequences of the binary tree, respectively. It is guaranteed that the binary tree can be uniquely determined by the input sequences. 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 binary tree, 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 87 2 3 4 6 5 1 85 3 7 2 6 4 8 12 68 17 912 -30 899 99Sample Output:
LCA of 2 and 6 is 3.8 is an ancestor of 1.ERROR: 9 is not found.ERROR: 12 and -3 are not found.ERROR: 0 is not found.ERROR: 99 and 99 are not found.题目大意:
- 给出m次查询与长度为n的前序序列。找到最近的公共祖先
思路:
不用建树,用 前序+中序转层序的办法,每一次都等读到一个根,如果a 和b就位于当前根的左边和右边。那么就是这个根了,如果都落在左子树就朝左子树找,如果都落在右子树就朝右子树那边找。特别的如果a 或b与当前根是一致的,要特判。
参考代码:
#include<vector>
#include<algorithm>
#include<map>
#include<cstdio>
using namespace std;
vector<int> pre, in;
map<int, int> mp;
void lca(int root, int st, int ed, int a, int b){
if(st > ed) return;
int now = mp[pre[root]], ra = mp[a], rb = mp[b];
if((ra < now && rb > now) || (ra > now && rb < now)) printf("LCA of %d and %d is %d.\n", a, b, in[now]);
else if(ra < now && rb < now) lca(root + 1, st, now - 1, a, b);
else if(ra > now && rb > now) lca(root + now - st + 1, now + 1, ed, a, b);
else if(ra == now || rb == now){
if(ra == now) swap(a, b);
printf("%d is an ancestor of %d.\n", b, a);
}
}
int n, m, a, b;
int main(){
scanf("%d%d", &m, &n);
pre.resize(n + 1); in.resize(n + 1);
for(int i = 1; i <= n; ++i) scanf("%d", &in[i]), mp[in[i]] = i;
for(int i = 1; i <= n; ++i) scanf("%d", &pre[i]);
for(int i = 0; i < m; ++i){
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]? a : b);
else lca(1, 1, n, a, b);
}
return 0;
}边栏推荐
- Graph theory and concept
- window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
- C # fine sorting knowledge points 10 generic (recommended Collection)
- Endnote 无法编辑range 解决
- 带你创建你的第一个C#程序(建议收藏)
- redis淘汰策列
- Spark提交参数--files的使用
- JVM-垃圾收集器详解
- Promise object and macro task, micro task
- Word 样式模板复制到另一文档
猜你喜欢

matlab 优化工具 manopt 安装

Implementation of asynchronous FIFO

Node learning

记一次Yarn Required executor memeory is above the max threshold(8192MB) of this cluster!

MySQL installation and configuration super detailed tutorial and simple database and table building method

谷歌云盘如何关联Google Colab

GAMES101复习:线性代数

MATLAB读取显示图像时数据格式转换原因

Spark partition operators partitionby, coalesce, repartition

你准备好脱离“内卷化怪圈”了吗?
随机推荐
Week303 of leetcode
How to understand the maximum allowable number of errors per client connection of MySQL parameters in Seata?
p4552-差分
ICPC2021昆明M-暴力+主席树
获取键盘按下的键位对应ask码
2021上海市赛-H-二分答案
GAMES101复习:三维变换
二进制补码
Tasks, micro tasks, queues and scheduling (animation shows each step of the call)
Spark DF增加一列
Notes on inputview and inputaccessoryview of uitextfield
Is it safe to open futures online? Which company has the lowest handling charge?
How spark gets columns in dataframe --column, $, column, apply
盒子躲避鼠标
数据系统分区设计 - 分区与二级索引
2019浙江省赛C-错排问题,贪心
The development summary of the function of fast playback of audio and video in any format on the web page.
Singleton mode 3-- singleton mode
Spark SQL null value, Nan judgment and processing
PAT甲级题目目录