1119 Pre- and Post-order Traversals
Suppose that all the keys in a binary tree are distinct positive integers. A unique binary tree can be determined by a given pair of postorder and inorder traversal sequences, or preorder and inorder traversal sequences. However, if only the postorder and preorder traversal sequences are given, the corresponding tree may no longer be unique.
Now given a pair of postorder and preorder traversal sequences, you are supposed to output the corresponding inorder traversal sequence of the tree. If the tree is not unique, simply output any one of them.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤ 30), the total number of nodes in the binary tree. The second line gives the preorder sequence and the third line gives the postorder sequence. All the numbers in a line are separated by a space.
Output Specification:
For each test case, first printf in a line Yes if the tree is unique, or No if not. Then print in the next line the inorder traversal sequence of the corresponding binary tree. If the solution is not unique, any answer would do. It is guaranteed that at least one solution exists. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the end of the line.

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int inf = ;
const int maxn = ; int n;
int pre[maxn],post[maxn];
int Tree[maxn][];
int flag=true;
int buildTree(int l1,int r1,int l2,int r2){
if(r1<l1) return -;
int root=pre[l1]; if(l1==r1){
Tree[root][]=-;
Tree[root][]=-;
return root;
} if(l1+==r1){
Tree[root][]=pre[l1+];
Tree[root][]=-;
flag=false;
return root;
} int i;
for(i=l2;post[i]!=pre[l1+];i++);
Tree[root][]=buildTree(l1+, l1++i-l2, l2, i);
Tree[root][]=buildTree(l1++i-l2, r1, i+, r2-);
return root;
}
int cnt=;
void inOrderTra(int v){
if(v==-){
return;
}
inOrderTra(Tree[v][]);
if(cnt) printf(" ");
printf("%d",v); cnt++;
inOrderTra(Tree[v][]);
} int main(){
fill(Tree[], Tree[]+maxn*, -); scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d",&pre[i]);
}
for(int i=;i<n;i++){
scanf("%d",&post[i]);
} int Root=buildTree(,n-,,n-);
if(flag) printf("Yes\n");
else printf("No\n");
inOrderTra(Root);
printf("\n");
}
1119 Pre- and Post-order Traversals的更多相关文章
- Construct a tree from Inorder and Level order traversals
Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is a ...
- [LeetCode] Rank Scores 分数排行
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- ASP.NET MVC : Action过滤器(Filtering)
http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...
- HDU 1160 FatMouse's Speed
半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- Spring Cloud Zuul 限流详解(附源码)(转)
在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal
Pre: node 先, Inorder: node in, Postorder: node 最后 PreOrder Inorde ...
随机推荐
- activemq 无法消费! consumers are alive when the messages are stuck !
我的微服务中, activemq 消费 一条消息的时候, 出了错, 结果导致了 那条消息就一直处于pending 状态, queue.user.545c2ed5-fee7-482a-bb59-564b ...
- SpringBoot 之热部署
默认情况下, 我们修改 class 或者 修改模板文件(templates目录 下面的文件) 等动态资源, 都不会立即自动生效. 在IDEA中, 我通过Ctrl + F9 , 仍然是无效. 当然, 静 ...
- 异步请求Ajax(取得json数据)
异步请求Ajax 没有学习Ajax之前请求数据的时候都是整个页面全部刷新了一次,也就是每次请求都会重新请求所有的资源.但是在很多时候不需要页面全部刷新,仅仅是需要页面的局部数据刷新即可,此时需要发送异 ...
- JDK中的注解简单了解
0.注解(注解是给编译器看的东东) 注解的定义方式是@Interface,注解属性定义是类似于普通类的方法定义的,注解属性赋值是使用default关键字完成的,如下图所示 注解在定义时可以给默认值,也 ...
- mysql,utf8,utf8mb4
参考文章 https://www.cnblogs.com/beyang/p/7580814.html https://blog.csdn.net/testcs_dn/article/details/7 ...
- 根据需要查找需要的第三方pyhton库
1.可以在https://awesome-python.com/这个网站上按照分类去寻找,上面收录了比较全面的第三方库.比如我们想要找爬虫方面的库时,查看Web Crawling这个分类,就能看到相应 ...
- 拜托!面试请不要再问我Spring Cloud底层原理[z]
[z]https://juejin.im/post/5be13b83f265da6116393fc7 拜托!面试请不要再问我Spring Cloud底层原理 欢迎关注微信公众号:石杉的架构笔记(id: ...
- NPOI 导入为table 处理excel 格式问题
ICell cell = row.GetCell(j); if (!cell.isDbNullOrNull()) { switch (cell.CellType) { case CellType.Bl ...
- java并发编程艺术
cas算法 概要 刚开始看这本书的时候很经常看到cas算法,个人觉得cas算法在并发编程中也是挺重要的的一部分,cas是比较并交换的意思(compare and swap),campareAndSwa ...
- Python下安装MySQLdb模块
----------------------[针对Windows下python 的MySQLdb模块安装]--------------------- 一.检查MySQLdb模块是否安装,可在DOS命令 ...