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 ...
随机推荐
- C# 操作符 << 与 >>
1.<< 左移操作符: 左移操作符,将第一个操作数向左移动第二个操作数指定的位数,空出的位置补0.左移相当于乘. 左移一位相当于乘2;左移两位相当于乘4;左移三位相当于乘8. 如:x< ...
- VS2015+Opencv3.2配置(一次配好)
对于 VS2015+QT5.8的配置我就不介绍了,由于我配置的比较早,具体有的东西忘掉了,大家可以参考下面这几篇文章. 留白留白留白留白留白(稍后补) 对于Opencv+VS的配置是我重点要说的内容. ...
- JavaScript获取、修改CSS样式合辑
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- English-英语日常交流语句
- windows from docker 安装部署spring jar包方法
1.安装docker for windows,去官网下载就可以了,按照官网安装 2.把jar和dockerfile放在一个目录下(target 目录下) Dockerfile: FROM java:8 ...
- k8s之创建etcd集群
主机规划 maste01——192.168.10.63 master02——192.168.10.64 node01——192.168.10.65 node02——192.168.10.66 1.为保 ...
- db2 SQL6036N解决办法
问题背景: 数据库在进行大量的运算和数据处理的过程中,IO.CPU等资源消耗非常高的时候,强制停止数据库.db2stop force 结果数据库命令迟迟没有响应.这个时候对数据库进行其他操作均无响应, ...
- jdango 使用oss存储
安装django-aliyun-oss2-storage-0.1.5.tar.gz settings文件添加 MEDIA_ROOT = os.path.join(BASE_DIR,'upload/') ...
- FortiGate数据流分析 debug flow
1.工具说明 在防火墙部署中,经常会遇到防火墙接收到了数据包,但并未进行转发.可以通过diagnose debug flow 命令来对数据包的处理过程进行跟踪,可以清晰查看数据包再各个功能模块内的处理 ...
- 大数据OLAP引擎对比
Presto:内存计算,mpp架构 PB级别数据 presto适合pb级的海量数据查询分析,不是说把pb的数据放进内存,比如一张pb表,查询count,vag这种有个特点,虽然数据很多,但是最终的 ...