Source:

PAT A1119 Pre- and Post-order Traversals (30 分)

Description:

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

Keys:

Attention:

  • 如果树中所有的分支结点都有两个孩子,那么由先序和后序可以唯一确定一棵二叉树;

Code:

 /*
Data: 2019-06-29 17:43:37
Problem: PAT_A1119 Pre- and Post-order Traversals
AC: 36:40 题目大意:
给出先序和后序遍历,输出任意中序遍历,并判断是否唯一 基本思路:
即由先序和后序是否可以唯一的构造一棵二叉树?
易知先序+中序,后序+中序,层序+中序,可以唯一的构造一棵二叉树,
为什么呢?因为已知根结点和中序遍历,能够求出根结点的左子树和右子树,进而递归的构造一棵二叉树
重点在于找到根结点的左子树和右子树,
先序遍历中,根结点root的下一个结点设为左子树的根结点lchild
后序遍历中,找到lchild,那么lchild之前(含root)的结点均为左子树,lchild之后直至root之前的结点均为右子树
这样,我们可以获得一棵二叉树;
如何判定是否唯一呢?
若分支结点既有左子树,又有右子树,那么我们可以通过上述方法唯一的构造一棵二叉树
若分支结点只有左子树或右子树
则我们既可以认为它是左子树,又可以认为它是右子树,这样就产生了多个解, 即此时二叉树不唯一
当我们总假设该子树为左子树,这样可以就获得其中一棵二叉树了
*/
#include<cstdio>
#include<vector>
using namespace std;
const int M=;
int pre[M],post[M],ans=;
vector<int> in; void Travel(int preL, int preR, int postL, int postR)
{
if(preL > preR){
ans=;
return;
}
if(preL == preR){
in.push_back(pre[preL]);
return;
}
int k;
for(k=postL; k<=postR; k++)
if(pre[preL+] == post[k])
break;
int numLeft = k-postL;
Travel(preL+,preL++numLeft,postL,k);
in.push_back(pre[preL]);
Travel(preL++numLeft+,preR,k+,postR-);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n;
scanf("%d", &n);
for(int i=; i<n; i++)
scanf("%d", &pre[i]);
for(int i=; i<n; i++)
scanf("%d", &post[i]);
Travel(,n-,,n-);
if(ans) printf("Yes\n");
else printf("No\n");
for(int i=; i<n; i++)
printf("%d%c", in[i],i==n-?'\n':' '); return ;
}

PAT_A1119 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. vjudge B - Design T-Shirt

    B - Design T-Shirt 思路:水题,模拟即可. #include<cstdio> #include<cstring> #include<iostream&g ...

  2. Spring MVC的@RequestMapping多个URL映射到同一个方法

    @RequestMapping可以是一个URL对应一个方法,也可以多个URL对应同一个方法,写法如下: @RequestMapping(value={"url","res ...

  3. 固定一个div在浏览器底部

    转自原文 如何固定一个div在浏览器底部   方法1:使用CSS绝对定位 div{ position:absolute; bottom:0px; left:0px; } 方法2:使用CSS固定定位 d ...

  4. 关于Openstack的浅层次认知

    Openstack浅析 英文好的应该直接跳到官方文档去看相关的介绍,以下是具体介绍的连接,包含Openstack的具体架构: http://docs.openstack.org/kilo/instal ...

  5. HDU 1561&HDU 3449 一类简单依赖背包问题

    HDU 1561.这道是树形DP了,所谓依赖背包,就是选A前必须选B,这样的问题.1561很明显是这样的题了.把0点当成ROOT就好,然后选子节点前必须先选根,所以初始化数组每一行为该根点的值.由于多 ...

  6. 1.7-BGP⑥

    BGP中的路由控制/过滤: LAB1:Distribute-list调用ACL(较落后) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      Step1:通过ACL定义 ...

  7. NGINX之——配置HTTPS加密反向代理訪问–自签CA

    转载请注明出处:http://blog.csdn.net/l1028386804/article/details/46695495 出于公司内部訪问考虑,採用的CA是本机Openssl自签名生成的,因 ...

  8. 用户向导左右滑动页面实现之ViewPager

    接着上一篇博客.上一篇博客是用ImageSwitcher实现用户向导功能,如今用ViewPager实现同样的功能. 直接看代码: 布局文件activity_main.xml <RelativeL ...

  9. Windows移动开发(五)——初始XAML

    关于详细的基本功就先说这么多.后面遇到再补充说明,前面说的都是一些代码和原理方面的东西.接下来说的会有界面和代码结合,会有成就感,由于能真正的做出东西来了. Windows移动开发包含Windows ...

  10. 小米手机 js 脚本取src为空的适配问题

    今天測试提上来一个问题 我android webview 中运行了一段js脚本.去替换原来的图片.可是小米手机上竟然没起作用 花了一个中午的午休看问题 贴出来帮助下遇到相同的问题的朋友吧.我百度了半天 ...