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.
前序和中序的解释:https://blog.csdn.net/ailunlee/article/details/80755357
代码:
#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)的更多相关文章

  1. 已知树的前序、中序,求后序的java实现&已知树的后序、中序,求前序的java实现

    public class Order { int findPosInInOrder(String str,String in,int position){ char c = str.charAt(po ...

  2. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  3. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...

  4. 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 ...

  5. c/c++ 用前序和中序,或者中序和后序,创建二叉树

    c/c++ 用前序和中序,或者中序和后序,创建二叉树 用前序和中序创建二叉树 //用没有结束标记的char*, clr为前序,lcr为中序来创建树 //前序的第一个字符一定是root节点,然后去中序字 ...

  6. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...

  7. 【2】【leetcode-105,106】 从前序与中序遍历序列构造二叉树,从中序与后序遍历序列构造二叉树

    105. 从前序与中序遍历序列构造二叉树 (没思路,典型记住思路好做) 根据一棵树的前序遍历与中序遍历构造二叉树. 注意:你可以假设树中没有重复的元素. 例如,给出 前序遍历 preorder = [ ...

  8. HDU 1710 (二叉树的前序和中序,求后序)

    题目链接 题目大意: 输入二叉树的前序.中序遍历,请输出它的后序遍历 #include <stdio.h> #include <string.h> ; // 长度为n s1 前 ...

  9. LeetCode:105_Construct Binary Tree from Preorder and Inorder Traversal | 根据前序和中序遍历构建二叉树 | Medium

    要求:通过二叉树的前序和中序遍历序列构建一颗二叉树 代码如下: struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode ...

随机推荐

  1. CentOS yum 安装nginx

    当使用以下命令安装Nginx时,发现无法安装成功 yum install -y nginx 需要做一点处理. 安装Nginx源 执行以下命令: rpm -ivh http://nginx.org/pa ...

  2. 理解JavaScript的原型链

    1. 什么是对象 在JavaScript中,对象是属性的无序集合,每个属性存放一个原始值.对象或函数. 1.1 创建对象 在JavaScript中创建对象的两种方法: ① 字面上: var myObj ...

  3. Spring Boot Logback 默认配置

    Spring Boot Logback 默认配置 标签(空格分隔): Spring Boot Intro(介绍) Spring Boot 1.5.9 默认使用的日志框架是 Logback. 生效的默认 ...

  4. C#LeetCode刷题之#171-Excel表列序号(Excel Sheet Column Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3852 访问. 给定一个Excel表格中的列名称,返回其相应的列序 ...

  5. 极简 Node.js 入门 - 2.3 process

    极简 Node.js 入门系列教程:https://www.yuque.com/sunluyong/node 本文更佳阅读体验:https://www.yuque.com/sunluyong/node ...

  6. Linux域名服务器部署

    环境:vmware workstation          系统:Red Hat7.4 DNS服务部署: IP地址:192.168.100.151        DNS:192.168.100.15 ...

  7. 什么是c/c++编译

    GCC是什么 GNU Compiler Collection的缩写,一开始是c语言的编译器,但现今可以支持多种语言的编译工作,也支持了多个硬件平台的编译.总而言之,主流的c语言编译器就是这个gcc了. ...

  8. python列表的增删改查和嵌套

    列表 python常用的数据类型 可承载任意的数据类型 列表是有序的,可索引.切片(步长) 列表的创建 list1 = [1, 2, 'whll'] #1. list2 = list() #2. #3 ...

  9. 7、TypeScript类型、接口、类、泛型综合使用 -- TypeScript封装统一操作Mysql Mongodb Mssql的底层类库。

    功能:定义一个操作数据库的库,支持Mysql Mssql Mongodb 要求:Mysql Mssql Mongodb功能一样 都有add.update.delete.get方法 注意:约束统一的规范 ...

  10. unity探索者之微信分享回调

    版权声明:本文为原创文章,转载请声明http://www.cnblogs.com/unityExplorer/p/7574561.html 上一遍讲了微信分享的一些坑,然后就忘了回调这事儿了,今天补上 ...