PAT_A1119 Pre- and Post-order Traversals
Source:
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
Yesif the tree is unique, orNoif 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的更多相关文章
- 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 ...
- [LeetCode] Rank Scores 分数排行
Write a SQL query to rank scores. If there is a tie between two scores, both should have the same ra ...
- HDU 4358 Boring counting(莫队+DFS序+离散化)
Boring counting Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 98304/98304 K (Java/Others) ...
- ASP.NET MVC : Action过滤器(Filtering)
http://www.cnblogs.com/QLeelulu/archive/2008/03/21/1117092.html ASP.NET MVC : Action过滤器(Filtering) 相 ...
- HDU 1160 FatMouse's Speed
半个下午,总算A过去了 毕竟水题 好歹是自己独立思考,debug,然后2A过的 我为人人的dp算法 题意: 为了支持你的观点,你需要从给的数据中找出尽量多的数据,说明老鼠越重速度越慢这一论点 本着“指 ...
- UVA 1175 Ladies' Choice 稳定婚姻问题
题目链接: 题目 Ladies' Choice Time Limit: 6000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu 问题 ...
- Spring Cloud Zuul 限流详解(附源码)(转)
在高并发的应用中,限流往往是一个绕不开的话题.本文详细探讨在Spring Cloud中如何实现限流. 在 Zuul 上实现限流是个不错的选择,只需要编写一个过滤器就可以了,关键在于如何实现限流的算法. ...
- [LeetCode] 系统刷题4_Binary Tree & Divide and Conquer
参考[LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal 可以对binary tree进行遍历. 此处说明Divi ...
- LeetCode: Recover Binary Search Tree 解题报告
Recover Binary Search Tree Two elements of a binary search tree (BST) are swapped by mistake. Recove ...
- [LeetCode] questions conlusion_InOrder, PreOrder, PostOrder traversal
Pre: node 先, Inorder: node in, Postorder: node 最后 PreOrder Inorde ...
随机推荐
- Mac下使用OpenMP
Mac下使用OpenMP,修改Build Options 下面的compiler for c/c++/objective-C 为 LLVM GCC 4.2 - Language 则可以找到Enable ...
- MSP430WARE++的使用2:RSP1 driver的调用方法
MSP430WARE是一套基于C++语言的开源的MSP430层次化软件架构,支持多种外设.本文将介绍雷达測速芯片RSP1驱动程序的调用方法. 1.硬件原理图 採用下图所看到的 ...
- javascript 使用方式
第一种:内嵌在html节点中 <html> <body> <input type="button" onclick="document.bo ...
- C++_homework_StackSort
顾名思义(?)类似于单调栈?维护一个单调递减的栈.一旦准备入栈的元素大于栈顶元素,栈一直弹出直到准备入栈的元素小于等于栈顶元素,弹出的元素压入另一个tmp栈中. #include <iostre ...
- IntelliJ IDEA :解决idea导入项目爆红
转:https://my.oschina.net/LevelCoder/blog/1802158 我们在导入一个新的项目到idea的时候,项目明明没有报错,但是会出现出了父包属于正常颜色外,其子包都会 ...
- Thinkpad E450c进入BIOS
重启系统,一直按F12,进入系统设置后,按tab进入App Menu选项卡,选择Setup按回车进入BIOS设置
- 模拟Queue(wait/notify)
BlockingQueue:顾名思义,首先它是一个队列,并且支持阻塞的机制,阻塞的放入和得到数据.我们要实现LinkedBlockingQueue下面的两个方法put和take. put(anObje ...
- HDU2186
2019-05-30 19:31:10 水题 #include <bits/stdc++.h> using namespace std; int main() { int c; scanf ...
- [BZOJ2321,LuoguP1861]星(之)器
丧心病狂的神仙题 丧心病狂的神仙题 丧心病狂的神仙题 显然,不管你怎么移动,答案都是一定的 然后我们很快能联系到物理里面的能量守恒,于是自然地我们要给每个点搞一个势能出来 然后把势能的表达式写出来就可 ...
- ROS-URDF-建立模型
前言:建立一个简单的urdf模型 详解请参看教程http://wiki.ros.org/urdf/Tutorials/Building%20a%20Visual%20Robot%20Model%20w ...