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. [bzoj3747][POI2015]Kinoman_线段树

    Kinoman bzoj-3747 POI-2015 题目大意:有m部电影,第i部电影的好看值为w[i].现在放了n天电影,请你选择一段区间l~r使得l到r之间的好看值总和最大.特别地,如果同一种电影 ...

  2. 高速搞定Eclipse的语法高亮

    编辑器背景颜色 打开Preferences 选择TextEditors 语法高亮配色 这里以Javascript为例. 选择Javascript 点击右边圈出的绿色框里的选项,适当改动颜色, 高亮色參 ...

  3. volatile非原子性示例

    volatile非原子性示例 学习了:<Java多线程编程核心技术>高洪岩 著 Page124 package com.stono.thread2.page124_2; public cl ...

  4. 美团网 KVM虚拟化公开课学习笔记

    KVM优化技术,美团开放平台--邱剑 基于KVM现有选项做一些优化.视频地址:http://www.osforce.cn/course/77/learn#lesson/80 CPU调优: 1.Cont ...

  5. 加密学教程(Cryptography Tuturials)文件夹

    加密学教程(Cryptography Tuturials) 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&quo ...

  6. android logo:内核、android开机动画【转】

    本文转载自: 关键词:Android 开机logo  开机动画 initlogo.rle   bootanimation  desc.txt 平台信息:内核:linux2.6/linux3.0系统:a ...

  7. Python 网络爬虫与信息获取(二)—— 页面内容提取

    1. 获取超链接 python获取指定网页上所有超链接的方法 links = re.findall(b'"((http|ftp)s?://.*?)"', html) links = ...

  8. springCloud多模块打包时报错问题

    执行mvn clean package spring-boot:repackage,报错如下: [ERROR] Failed to execute goal org.springframework.b ...

  9. CDH版Phoenix的安装(图文详解)

    不多说,直接上干货! 写在前面的话 我这里,四个节点的bigdata集群.分别为cmbigdata1.cmbigdata2.cmbigdata3和cmbigdata4. https://i.cnblo ...

  10. Tomcat 程序无问题的情况下页面打开变慢的原因

    看看这写日志的频率就知道我有多闲了.. 前言: 其实关于tomcat,遇到过很多关于“慢”的问题,比如启动慢,比如页面打开慢, 以前太忙也太懒,不愿意花时间分析原因,现在终于肯静下来找原因 环境是ec ...