当前位置:网站首页>Pat grade a 1151 LCA in a binary tree (30 points)
Pat grade a 1151 LCA in a binary tree (30 points)
2022-07-25 15:33:00 【nekoha_ dexter】
1151 LCA in a Binary Tree (30 branch )
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.The main idea of the topic :
- give m Query times and length is n The preorder sequence of . Find the nearest common ancestor
Ideas :
You don't have to build a tree , use Before the order + The method of transforming middle sequence into sequence , Every time I read a root , If a and b On the left and right of the current root . So this is the root , If it all falls on the left sub tree, look for it , If they all fall on the right subtree, look towards the right subtree . Special if a or b Consistent with the current root , A special sentence is required .
Reference code :
#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;
}Code reference :https://blog.csdn.net/liuchuo/article/details/82560863
边栏推荐
猜你喜欢

谷歌云盘如何关联Google Colab

图论及概念

Spark memory management mechanism new version

Remember that spark foreachpartition once led to oom

matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值

window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28

Yan required executor memory is above the max threshold (8192mb) of this cluster!

NPM's nexus private server e401 E500 error handling record

ML - natural language processing - Basics

盒子躲避鼠标
随机推荐
Xcode added mobileprovision certificate file error: Xcode encoded an error
UITextField的inputView和inputAccessoryView注意点
Spark partition operators partitionby, coalesce, repartition
ML - 语音 - 语音处理介绍
带你创建你的第一个C#程序(建议收藏)
数据系统分区设计 - 分区再平衡(rebalancing)
Idea eye care settings
ML - Speech - advanced speech model
Implementation of asynchronous FIFO
UIDocumentInteractionController UIDocumentPickerViewController
Spark SQL null value, Nan judgment and processing
window系统黑窗口redis报错20Creating Server TCP listening socket *:6379: listen: Unknown error19-07-28
4PAM在高斯信道与瑞利信道下的基带仿真系统实验
CF365-E - Mishka and Divisors,数论+dp
二进制补码
伤透脑筋的CPU 上下文切换
获取键盘按下的键位对应ask码
CF685B-求有根树每颗子树的重心
No tracked branch configured for branch xxx or the branch doesn‘t exist. To make your branch trac
Record a redis timeout