pat1086. Tree Traversals Again (25)
1086. Tree Traversals Again (25)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.
Output Specification:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
Sample Input:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
Sample Output:
3 4 2 6 5 1
#include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
int per[],in[];
stack<int> s;
int n;
void Build(int *per,int *in,int num){
if(num==){
return;
}
int mid=per[]; //cout<<mid<<endl; int i;
for(i=;i<num;i++){
if(in[i]==mid){
break;
}
} //cout<<num<<endl; Build(per+,in,i);
Build(per++i,in++i,num--i);
if(num==n){
printf("%d",mid);
}
else{
printf("%d ",mid);
}
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
scanf("%d",&n);
n*=;
int i,num,pi=,ini=;
string op;
for(i=;i<n;i++){
cin>>op;
if(op=="Push"){
scanf("%d",&num);
s.push(num);
per[pi++]=num;
}
else{
in[ini++]=s.top();
s.pop();
}
}
n=n/; /*cout<<n<<endl;
for(i=0;i<n;i++){
cout<<per[i]<<" ";
}
cout<<endl;
for(i=0;i<n;i++){
cout<<in[i]<<" ";
}
cout<<endl;*/ Build(per,in,n);
printf("\n");
return ;
}
pat1086. Tree Traversals Again (25)的更多相关文章
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT1086:Tree Traversals Again
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历
03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorde ...
- pat03-树3. Tree Traversals Again (25)
03-树3. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PTA 03-树3 Tree Traversals Again (25分)
题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again (25分) An inor ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)
7-5 Tree Traversals Again (25 分) An inorder binary tree traversal can be implemented in a non-recu ...
- 1086. Tree Traversals Again (25)
题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
随机推荐
- oracle安装和使用问题解决方案
1.Enter the full pathname for java.exe 要输入的是32位系统的jdk中的java.exe路径,比如 C:\Program Files\Java\jdk1.6.0 ...
- 浅聊本人学习React的历程——第一篇生命周期篇
作为一个前端小白,在踏入前端程序猿行业的第三年接触了React,一直对于框架有种恐惧感,可能是对陌生事物的恐惧心里吧,导致自己一直在使用原生JS和JQ作为开发首选,但是在接触了React之后,发现了其 ...
- linux定制的补充
上一篇博文:http://www.cnblogs.com/hjc4025/p/6918323.html 这篇文章是对之前博文的一点扩展和补充: 这里主要是在之前的基础上添加了一些自己制作好的程序,还有 ...
- UINavigationController + UIScrollView组合,视图尺寸的设置探秘(三)
还是在苹果的 View Controller Catalog for iOS 文章中找到答案.文中提到了两点: 1.If the navigation bar or toolbar are visib ...
- AISing Programming Contest 2019C(DFS,BFS)
#include<bits/stdc++.h>using namespace std;int n,m;long long a=0,b=0,ans=0;char s[407][407];in ...
- Crontab 驱动 Scrapy 定时任务
做了个爬虫去定时抓自己发布在cnblogs更新的文章,考虑用corntab定时任务驱动 crontab 任务配置 crontab配置规则就不啰嗦了,网上很多介绍,规则很容易功能却很强大. 然后我发现只 ...
- UVA10173 Smallest Bounding Rectangle 最小面积矩形覆盖
\(\color{#0066ff}{题目描述}\) 给定n(>0)二维点的笛卡尔坐标,编写一个程序,计算其最小边界矩形的面积(包含所有给定点的最小矩形). 输入文件可以包含多个测试样例.每个测试 ...
- 如何实现 MySQL 的读写分离?MySQL 主从复制原理的是啥?如何解决 MySQL 主从同步的延时问题?
如何实现 MySQL 的读写分离? 其实很简单,就是基于主从复制架构,简单来说,就搞一个主库,挂多个从库,然后我们就单单只是写主库,然后主库会自动把数据给同步到从库上去. MySQL 主从复制原理的是 ...
- CF671A Recycling Bottles 计算几何
It was recycling day in Kekoland. To celebrate it Adil and Bera went to Central Perk where they can ...
- java程序设计实验
建立文件调试jdk idea断点调试 项目素数的寻遍