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. 零基础学python-4.2 其它内建类型

    这一章节我们来聊聊其它内建类型 1.类型type 在python2.2的时候,type是通过字符串实现的,再后来才把类型和类统一 我们再次使用上一章节的图片来说明一些问题 我们通过对照上面的图片.在p ...

  2. CSS3选择器(全)

    CSS选择器复习 通用选择器:* 选择到全部的元素 选择子元素:> 选择到元素的直接后代(第一级子元素) 相邻兄弟选择器:+ 选择到紧随目标元素后的第一个元素 普通兄弟选择器:~ 选择到紧随其后 ...

  3. c++ 11 thread 初试

    最新的 c++11标准整合进了 线程支持.以下写一个小程序測试一下. 測试代码: #include <iostream> #include <thread> void hell ...

  4. UVA10254 - The Priest Mathematician(找规律)

    UVA10254 - The Priest Mathematician(找规律) 题目链接 题目大意:4根柱子的汉诺塔. 解题思路:题目里面有提示,先借助四个柱子移走k个,然后在借助三个柱子移走剩余的 ...

  5. ExtJs 下拉单联动,次级下拉框查询模式

    queryMode : 'local' 如果下拉框的值是本地数据,最好设定queryMode为local,这样可以提高用户的响应速度

  6. jQuery - 点击图片加边框

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  7. 【NOIP2018】为什么这么无力啊

    菜鸡又要爆零了 辛辛苦苦背板子结果考时候脑子一片空白 第一题线段树调了半小时 看完三道题两道写暴搜一道写暴力(说是暴搜,觉得更像写了个背包) 别提暴搜还忘记剪枝. . . . . . 我觉得考场上最菜 ...

  8. 请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码。

    请问在C#的Winform下如何用正则表达式限制用户只能在textBox中输入18位的身份证号码. 2013-06-18 11:07会飞的鱼儿18 | 分类:C#/.NET | 浏览101次 不能有空 ...

  9. hdu 1698(线段树区间更新)

    Just a Hook Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  10. python 5:str(某一变量)(将其他数字解释为字符串)

    age = messege = "Your's age is " + str(age) #将其他数字更改为字符串 print(messege) 运行结果应该是: Your's ag ...