PAT甲级1151(由前序和中序确定LCA)
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.
前序和中序的解释:https://blog.csdn.net/ailunlee/article/details/80755357
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<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<vector>
#include<cmath> const int maxn=1e5+;
typedef long long ll;
using namespace std; map<int,int>pos;
int in[maxn],pre[maxn];
void LCA(int l,int r,int preroot,int a,int b)
{
if(l>r)
{
return;
}
int Root=pos[pre[preroot]];
int ina=pos[a];
int inb=pos[b];
if(ina<Root&&inb<Root)
{
LCA(l,Root-,preroot+,a,b);
}
else if(ina>Root&&inb>Root)
{
LCA(Root+,r,preroot+Root-l+,a,b);
}
else if ((ina < Root && inb> Root) || (ina > Root && inb < Root))
{
printf("LCA of %d and %d is %d.\n", a, b, in[Root]);
return ;
}
else if (ina == Root)
{
printf("%d is an ancestor of %d.\n", a, b);
return ;
}
else if (inb == Root)
{
printf("%d is an ancestor of %d.\n", b, a);
return ;
}
} int main()
{
int m,n;
cin>>m>>n;
for(int t=;t<=n;t++)
{
scanf("%d",&in[t]);
pos[in[t]]=t;
}
for(int t=;t<=n;t++)
{
scanf("%d",&pre[t]);
}
int a,b;
while(m--)
{
scanf("%d%d",&a,&b);
if(pos[a]==&&pos[b]==)
{
printf("ERROR: %d and %d are not found.\n",a,b);
}
else if(pos[a]==&&pos[b]!=)
{
printf("ERROR: %d is not found.\n",a);
}
else if(pos[a]!=&&pos[b]==)
{
printf("ERROR: %d is not found.\n",b);
}
else
{
LCA(,n,,a,b);
}
}
return ;
}
PAT甲级1151(由前序和中序确定LCA)的更多相关文章
- 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现
public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...
- Java实现二叉树的前序、中序、后序遍历(非递归方法)
在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...
- LeetCode二叉树的前序、中序、后序遍历(递归实现)
本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal 由前序和中序遍历建立二叉树 C++
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- c/c++ 用前序和中序,或者中序和后序,创建二叉树
c/c++ 用前序和中序,或者中序和后序,创建二叉树 用前序和中序创建二叉树 //用没有结束标记的char*, clr为前序,lcr为中序来创建树 //前序的第一个字符一定是root节点,然后去中序字 ...
- Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)
在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...
- 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树
105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...
- HDU 1710 (二叉树的前序和中序,求后序)
题目链接 题目大意: 输入二叉树的前序.中序遍历,请输出它的后序遍历 #include <stdio.h> #include <string.h> ; // 长度为n s1 前 ...
- LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium
要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...
随机推荐
- python字符串和数字的基本运算符
python字符穿的4种表达方式 name='张三' name="张三" name='''张三''' name="""张三""&q ...
- Jmeter(十九) - 从入门到精通 - JMeter监听器 -上篇(详解教程)
1.简介 监听器用来监听及显示JMeter取样器测试结果,能够以树.表及图形形式显示测试结果,也可以以文件方式保存测试结果,JMeter测试结果文件格式多样,比如XML格式.CSV格式.默认情况下,测 ...
- “随手记”开发记录day20
练习软件的展示,尽量将软件全方面的展示给大众,希望不要像上次一样有许多遗漏的地方,让其他团队以为我们的软件没有完善的功能.
- 漫画 | 到底是什么让IT人如此苦逼???
写在最后 漫画是有点夸张,不过多少还是有点现实开发过程的影子! 老板很乐观,核心就是三个月上线,至于怎么办那是底下人的事. 产品很无奈,心里盘算着,万万不可在他这一环节耽误了进度,时间这么赶,先出个壳 ...
- Java—io流之打印流、 commons-IO
打印流 打印流根据流的分类: 字节打印流 PrintStream 字符打印流 PrintWriter /* * 需求:把指定的数据,写入到printFile.txt文件中 * * 分析: * 1, ...
- ALGEBRA-1 向量空间
向量空间对加法封闭 对数乘封闭 直和:表示的唯一性
- Django中信号signal针对model的使用
Django中实现对数据库操作的记录除了使用[开源插件]还可以使用信号signal独立实现 信号机制-观察者模式-发布与订阅:signal - 配置 # 文件路径:Django/myapps/__in ...
- C#设计模式之12-代理模式
代理模式(Proxy Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/411 访问. 代理模式属于结构型 ...
- vue或者js中平均分割数组
vue中 把一段长数组按照指定份数 均分 sliceArray(array, size) { var result = []; for (var x = 0; x < Math.ceil(arr ...
- 只要动手就能学到东西4 JSON.stringify
今天在调试javascript时,需要将不可显示字符如\r\n在console显示出来,查了下,原来可以用JSON.stringify().这个方法以前也用过,但都是将javascript对象变成js ...