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 ...
随机推荐
- dojo1.7 加载器
原文地址:http://dojotoolkit.org/documentation/tutorials/1.7/modules/ dojo现在支持在异步模块异步(AMD)定义中加入模块写入功能了,这使 ...
- 读取txt里面的数据进行计算
双在论坛上找到一个问题,有关读取txt里面的数据进行计算的问题. 尝试解决这个问题,获取每一行的X和Y的浮点数据即可.读取文本文件每一行,判断是否为空行,是否符以分隔符号(,)分隔的两个数值.每个数值 ...
- EXCEL跨表比较两列,并填充新值背景
Sub FillNewToYellow() Dim dic Dim oldArr(), updatedArr() On Error Resume Next If Worksheets("ol ...
- UWP&WP8.1图片照片添加水印
水印可以自己自己制作,也可以用代码写. 我这里主要写如何添加到照片上面. UWP和WP8.1添加的方法一样.代码是通用的. UWP和WP8.1没有像WPF和WINFROM中darw这样简便的API可以 ...
- Percona5.6源码安装
---恢复内容开始--- 系统环境:CentOS 6.8 1.安装依赖包 yum install gcc-c++ make cmake bison bison-devel ncurses-devel ...
- Sublime3插件安装
首先声明一下,小编是做后台开发出身,但是总是想捣鼓一些小的网站出来,可能是完美心作祟,感觉前端这边不能差事,所以就自己上了,一开始是用eclipse来开发的,具体原因忘了,也不知道怎么就开始用Subl ...
- 老男孩Day5作业:电子银行购物商城
1.作业需求: 模拟实现一个ATM + 购物商城程序 额度 15000或自定义 实现购物商城,买东西加入 购物车,调用信用卡接口结账 可以提现,手续费5% 支持多账户登录支持账户间转账 记录每月日常消 ...
- NOIWC2019游记
更新完了? ghj1222这个智障因为NOIP考的太菜没有去THUWC和PKUWC,但是NOIWC还是苟进去了 由于已经结束了,好多事实忘了,所以可能不完整 2019/1/23 Wednesday 明 ...
- CF1101C Division and Union 线段相交问题
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #i ...
- Session_Start
第一次启动浏览器后,执行了包含Session_Start的方法时触发,当关闭浏览器或者SESSION超时后,自动销毁.