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.

Sample Input 1:

7

1 2 3 4 6 7 5

2 6 7 4 5 3 1

Sample Output 1:

Yes

2 1 6 4 7 3 5

Sample Input 2:

4

1 2 3 4

2 4 3 1

Sample Output 2:

No

2 1 3 4

#include<iostream>//比较难,由前序和后序遍历来确定二叉树,参考了柳婼的
#include<vector>
using namespace std;
vector<int> pre, post, in;
bool unique=true;
void getin(int prel, int prer, int postl, int postr){
if(prel==prer){
in.push_back(pre[prel]);
return ;
}
if(pre[prel]==post[postr]){
int i=prel+1;
while(i<=prer&&pre[i]!=post[postr-1]) i++;
if(i-prel>1)
getin(prel+1, i-1, postl, postl+i-prel-2);
else
unique=false;
in.push_back(pre[prel]);
getin(i, prer, postl+i-prel-1, postr-1);
}
}
int main(){
int n;
cin>>n;
pre.resize(n), post.resize(n);
for(int i=0; i<n; i++)
cin>>pre[i];
for(int i=0; i<n; i++)
cin>>post[i];
getin(0, n-1, 0, n-1);
unique==true?cout<<"Yes"<<endl<<in[0]:cout<<"No"<<endl<<in[0];
for(int i=1; i<in.size(); i++)
cout<<" "<<in[i];
cout<<endl;
return 0;
}

PAT 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. PAT 1119 Pre- and Post-order Traversals [二叉树遍历][难]

    1119 Pre- and Post-order Traversals (30 分) Suppose that all the keys in a binary tree are distinct p ...

  3. [LeetCode] Rank Scores 分数排行

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

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

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

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

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

  6. HDU 1160 FatMouse's Speed

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

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

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

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

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

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

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

随机推荐

  1. LaTeX 在线编辑器(LaTeX online editors)

    eqneditor:有强大的几乎所有常用的数学符号对应的图标形式,便于快速完成latex公式编辑且易于粘贴拷贝. 此外,更为重要的一点是,随着编辑窗口内公式的编辑,会在页面的底部,自动生成其对应的 h ...

  2. JSP-Runoob:JSP 异常处理

    ylbtech-JSP-Runoob:JSP 异常处理 1.返回顶部 1. JSP 异常处理 当编写JSP程序的时候,程序员可能会遗漏一些BUG,这些BUG可能会出现在程序的任何地方.JSP代码中通常 ...

  3. 50.Ext_数字输入框_Ext.form.NumberField

    转自:https://blog.csdn.net/inflaRunAs/article/details/84033618 <mce:script type="text/javascri ...

  4. Filter,Interceptor和Aspect

    过滤器使用的主要是反射 :拦截器使用的主要是回调 :AOP使用的主要是动态代理. 一个请求过来 ,先进行过滤器处理,看程序是否受理该请求.过滤器放过后, 程序中的拦截器进行处理,处理完后进入被AOP动 ...

  5. PCB 规则引擎之JSON对象查看器

    在PCB规则引擎开发中,JavaScript V8引擎是处理业务逻辑的, 当然业务逻辑需要数据支撑才行,  即需有将数据推进入到V8引擎.目前这边数据传输到JavaScript V8引擎以C# Mod ...

  6. Android内存管理(11)*常见JVM回收机制「Java进程内存堆分代,JVM分代回收内存,三种垃圾回收器」

    参考: http://www.blogjava.net/rosen/archive/2010/05/21/321575.html 1,Java进程内存堆分代: 典型的JVM根据generation(代 ...

  7. Linux-fork()函数详解,附代码注释

    // // main.c // Project_C // // Created by LiJinxu on 16/8/13. // Copyright © 2016年 LiJinxu-NEU. All ...

  8. 322 Coin Change 零钱兑换

    给定不同面额的硬币(coins)和一个总金额(amount).写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合方式能组成总金额,返回-1.示例 1:coins = [1, ...

  9. [转]通过Net Manager 配置Oracle 11g本地监听服务(listener service)

    本文转自:http://blog.csdn.net/mozart_cai/article/details/8596504 [Target] 通过ip address 监听orcl服务,而不是通过loc ...

  10. C++ Primer(第4版)-学习笔记-第2部分:容器和算法

    第9章 顺序容器 顺序容器和关联容器 顺序容器内的元素按其位置存储和访问. 关联容器,其元素按键(key)排序. 顺序容器(sequential container). 顺序容器的元素排列次序与元素值 ...