当前位置:网站首页>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;
}边栏推荐
- GAMES101复习:线性代数
- UIDocumentInteractionController UIDocumentPickerViewController
- Reflection - Notes
- <栈模拟递归>
- 为什么PrepareStatement性能更好更安全?
- Instance tunnel use
- 2019浙江省赛C-错排问题,贪心
- Delayed loading source code analysis:
- C#精挑整理知识要点9 集合2(建议收藏)
- No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
猜你喜欢
随机推荐
Find out what happened in the process of new
The development summary of the function of fast playback of audio and video in any format on the web page.
MySQL installation and configuration super detailed tutorial and simple database and table building method
2019陕西省省赛J-位运算+贪心
PAT甲级题目目录
小波变换--dwt2 与wavedec2
MySQL heap table_ MySQL memory table heap Usage Summary - Ninth Five Year Plan small pang
Spark memory management mechanism new version
The difference between Apple buy in and apple pay
MATLAB 如何生产随机复序列
2021江苏省赛A. Array-线段树,维护值域,欧拉降幂
记一次redis超时
p4552-差分
ML - 图像 - 深度学习和卷积神经网络
带你创建你的第一个C#程序(建议收藏)
Idea remotely submits spark tasks to the yarn cluster
数据系统分区设计 - 请求路由
C # fine sorting knowledge points 9 Set 2 (recommended Collection)
解决DBeaver SQL Client 连接phoenix查询超时
C language function review (pass value and address [binary search], recursion [factorial, Hanoi Tower, etc.))








