PAT 甲级 1151 LCA in a Binary Tree
https://pintia.cn/problem-sets/994805342720868352/problems/1038430130011897856
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 8
7 2 3 4 6 5 1 8
5 3 7 2 6 4 8 1
2 6
8 1
7 9
12 -3
0 8
99 99
Sample 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.
代码:
#include <bits/stdc++.h>
using namespace std; int N, M;
vector<int> in, pre;
map<int, int> pos; void lca(int inl, int inr, int preroot, int a, int b) {
if(inl > inr) return ;
int inroot = pos[pre[preroot]], ain = pos[a], bin = pos[b];
if(ain < inroot && bin < inroot)
lca(inl, inroot - 1, preroot + 1, a, b);
else if((ain > inroot && bin < inroot) || (ain < inroot && bin > inroot))
printf("LCA of %d and %d is %d.\n", a, b, in[inroot]);
else if(ain > inroot && bin > inroot)
lca(inroot + 1, inr, preroot + 1 + inroot - inl, a, b);
else if(ain == inroot)
printf("%d is an ancestor of %d.\n", a, b);
else if(bin == inroot)
printf("%d is an ancestor of %d.\n", b, a);
} int main() {
scanf("%d%d", &M, &N);
in.resize(N + 1), pre.resize(N + 1);
for(int i = 1; i <= N; i ++) {
scanf("%d", &in[i]);
pos[in[i]] = i;
}
for(int i = 1; i <= N; i ++)
scanf("%d", &pre[i]); for(int i = 0; i < M; i ++) {
int x, y;
scanf("%d%d", &x, &y);
if(pos[x] == 0 && pos[y] == 0)
printf("ERROR: %d and %d are not found.\n", x, y);
else if(pos[x] == 0 || pos[y] == 0)
printf("ERROR: %d is not found.\n", pos[x] == 0 ? x : y);
else
lca(1, N, 1, x, y);
}
return 0;
}
LCA
FHFHFH
PAT 甲级 1151 LCA in a Binary Tree的更多相关文章
- PAT甲级|1151 LCA in a Binary Tree 先序中序遍历建树 lca
给定先序中序遍历的序列,可以确定一颗唯一的树 先序遍历第一个遍历到的是根,中序遍历确定左右子树 查结点a和结点b的最近公共祖先,简单lca思路: 1.如果a和b分别在当前根的左右子树,当前的根就是最近 ...
- PAT Advanced 1151 LCA in a Binary Tree (30) [树的遍历,LCA算法]
题目 The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both ...
- PAT 1151 LCA in a Binary Tree[难][二叉树]
1151 LCA in a Binary Tree (30 分) The lowest common ancestor (LCA) of two nodes U and V in a tree is ...
- 【PAT 甲级】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 bo ...
- 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 ...
- 1151 LCA in a Binary Tree
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 1151 LCA in a Binary Tree (30point(s))
The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U ...
- 【PAT甲级】1102 Invert a Binary Tree (25 分)(层次遍历和中序遍历)
题意: 输入一个正整数N(<=10),接着输入0~N-1每个结点的左右儿子结点,输出这颗二叉树的反转的层次遍历和中序遍历. AAAAAccepted code: #define HAVE_STR ...
- PAT_A1151#LCA in a Binary Tree
Source: PAT A1151 LCA in a Binary Tree (30 分) Description: The lowest common ancestor (LCA) of two n ...
随机推荐
- VC编译连接选项详解
VC编译连接选项详解 大家可能一直在用VC开发软件,但是对于这个编译器却未必很了解.原因是多方面的.大多数情况下,我们只停留在“使用”它,而不会想去“了解”它.因为它只是一个工具,我们宁可把更多的精力 ...
- RTTI(运行时类型识别)
C++为了能够在运行时正确判断一个对象确切的类型,加入了RTTI和type_info. type_info 为每一个类型增加一个type_info对象. 为了能够在运行时获得对象的类型信息type_i ...
- TMS320VC5509启动模式选择
1. TMS320VC5509内部没有存储空间,所以需要外部接flash.如果使用JTAG仿真板子的话,应该是选择USB下载模式,同时EEPROM应该是支持SPI FALSH的.
- MySQL数据库常用操作语法
1. 数据库初始化配置 1.1. 创建数据库 create database apps character set utf8 collate utf8_bin;创建数据库”app“,指定编码为utf8 ...
- 【Python学习笔记之三】lambda表达式用法小结
除了def语句之外,Python还提供了一种生成函数对象的表达式形式.由于它与LISP语言中的一个工具很相似,所以称为lambda.就像def一样,这个表达式创建了一个之后能够调用的函数,但是它返回了 ...
- 手动搭建openstack的痛苦经历
openstack真的是一个十分痛苦的东西,好在有自动部署工具,虽然有自动部署工具可以方便我们部署使用,但是学习的话,第一次最好手动部署,因为手动部署更能我们了解openstack的工作流程和各组建之 ...
- ecCodes 学习 利用ecCodes Python API对GRIB文件进行读写
参考 https://www.ecmwf.int/assets/elearning/eccodes/eccodes2/story_html5.htmlhttps://confluence.ecmwf. ...
- SAO Utils – SAO风格启动菜单
SAO Utils 是一款拥有 SAO(刀剑神域)外观风格的启动器,搭载各种各样强大的小工具. 随时随地.在屏幕任何地方 按住鼠标左键和右键并向下拖动 即可呼出应用启动菜单(触控设备直接支持双指下滑手 ...
- Laya1.x Timer小记
Timer是时钟管理类,在Laya初始化的时候会创建一个实例,通过Laya.timer访问. TimerHandler TimerHandler是对每一个定时任务的封装,每次调用frameOnce.f ...
- 廖雪峰git教程学习笔记
对git来说,没有消息就是最好的消息 使用 git init 把当前目录变为git仓库 要在仓库里加入文件,先在仓库目录新建这个文件后,比如新建一个文件xiaobai.txt,内容为: 在命令行里输入 ...