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 ...
随机推荐
- python基础学习1-函数相关
#!/usr/bin/env python # -*- coding:utf-8 -*- 函数相关 def f1(par,par2,par3="ok"): #定义函数 带参数,带参 ...
- 18-[模块]-shutil
shutil模块 高级的 文件.文件夹.压缩包 处理模块 (1)文件操作 shutil.copyfileobj(fsrc, fdst[, length]) 将文件内容拷贝到另一个文件中 import ...
- CF 1041 F. Ray in the tube
F. Ray in the tube 链接 题意: 有两条平行于x轴的直线A,B,每条直线上的某些位置有传感器.你需要确定A,B轴上任意两个整点位置$x_a$,$x_b$,使得一条光线沿$x_a→x_ ...
- zhengruioi 470 区间
区间 链接 题意:给定n个区间[li,ri].你可以选出任意一些区间,设选出的区间个数是s,[l,r]是这些区间的交,求min(s,r-l+1)的最大值. N≤3×105 分析:首先按照左端点排序,然 ...
- 微信小程序:选项卡页面切换
一.功能描述 在同一个页面内实现不同展示页面的切换功能,如下图所示 二.代码实现 1. index.js Page({ /** * 页面的初始数据 */ data: { currentData : 0 ...
- idea maven javaweb项目迁移时的maven和版本报错问题解决(可解决同类错误)
项目中代码红线报版本不支持xx语法,只需要将java版本设置为当前机器使用的java版本即可 这里我使用的是idea自带的maven,如果是自己安装的maven需要在 home directory 处 ...
- .net如何发送格式化的文本内容
MailMessage mailMessage = new MailMessage();ArrayList attachsendObject = new ArrayList();string mail ...
- 爬取代理IP
现在爬虫好难做啊,有些网站直接封IP,本人小白一个,还没钱,只能找免费的代理IP,于是去爬了西刺免费代理,结果技术值太低,程序还没调试好, IP又被封了... IP又被封了... IP又被封了... ...
- Python学习之路:MINST实战第一版
1.项目介绍: 搭建浅层神经网络完成MNIST数字图像的识别. 2.详细步骤: (1)将二维图像转成一维,MNIST图像大小为28*28,转成一维就是784. (2)定义好神经网络的相关参数: # M ...
- Python浮点算术:争议和限制
浮点数在计算机硬件中表示为以 2 为基数(二进制)的小数.举例而言,十进制的小数 0.125 等于 1/10 + 2/100 + 5/1000 ,同理,二进制的小数 0.001 等于0/2 + 0/4 ...