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的更多相关文章

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

  2. [LeetCode] Rank Scores 分数排行

    Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...

  3. HDU 4358 Boring counting(莫队+DFS序+离散化)

    Boring counting Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 98304/98304 K (Java/Others) ...

  4. ASP.NET MVC : Action过滤器(Filtering)

    http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...

  5. HDU 1160 FatMouse's Speed

    半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...

  6. UVA 1175 Ladies' Choice 稳定婚姻问题

    题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...

  7. Spring Cloud Zuul 限流详解(附源码)(转)

    在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...

  8. [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer

    参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...

  9. LeetCode: Recover Binary Search Tree 解题报告

    Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...

  10. [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal

    Pre: node 先,                      Inorder:   node in,           Postorder:   node 最后 PreOrder Inorde ...

随机推荐

  1. 团队第三次 # scrum meeting

    github 本此会议项目由PM召开,召开时间为4-7日晚上9点 召开时长15分钟 任务表格 袁勤 继续学习SpringBoot https://github.com/buaa-2016/phyweb ...

  2. svn Advanced

    1.Eclipse last 1.Eclipse 安装subversive插件: 安装连接器: 打开视图"SVN Repositories": 创建“资源库位置”: 配置全局范围文 ...

  3. [UnityAPI]SerializedObject类 & SerializedProperty类

    以Image类为例 1.MyImage.cs using UnityEngine; using UnityEngine.UI; public class MyImage : Image { ; pro ...

  4. 《算法导论》——计数排序Counting Sort

    今天贴出的算法是计数排序Counting Sort.在经过一番挣扎之前,我很纠结,今天这个算法在一些scenarios,并不是最优的算法.最坏情况和最好情况下,时间复杂度差距很大. 代码Countin ...

  5. background-position,有逗号和没逗号:截然不同的结果

    写星级评分demo的时候,发现background-position这个属性有点神奇 如上图,在其值中加“,”与不加“,”会带来截然不同的结果,来研究一哈,顺便把这个属性复习一下 1.backgrou ...

  6. spring jdbc学习1

    1.queryForObject - 其中的 RowMapper 指定如何去映射结果集的行, 常用的实现类为 BeanPropertyRowMapper - 使用 SQL 中列的别名完成列名和类的属性 ...

  7. leetcode5

    public class Solution { private int lo, maxLen; public String LongestPalindrome(String s) { int len ...

  8. Beta冲刺——第四天

    beat冲刺:第四天 各个成员今日完成的任务 成员 冯晓.马思远 彭辉.王爽 吴琼.郝延婷 今日完成任务 ·管理员功能模块的代码规范与测试 ·后台审稿系统代码规范 ·代码规范 ·系统审稿模块功能测试 ...

  9. python 关于文件夹的操作

    在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() #  path 表示文 ...

  10. Linux jdk安装

    Linux上一般会安装Open JDK,关于OpenJDK和JDK的区别:http://www.cnblogs.com/sxdcgaq8080/p/7487369.html 下面开始安装步骤: --- ...