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 ;
}
最新文章
- PHP缓存技术
- block fomating context
- 【经验记录】Jconsole Jvisualvm 监控Tomcat
- Swift3.0语言教程获取字符
- __declspec关键字详细用法
- CentOS学习笔记&mdash;软件管理程序RPM、YUM
- JavaScript中的事件冒泡机制
- GMT 绘制台站分布图
- Scikit-Learn与决策树
- 【BZOJ3884】上帝与集合的正确用法(欧拉定理,数论)
- struts2第一章-基本用法
- https协议的简单理解
- python 之 知识点(1)
- day05 Spring中自定义注解的用处-之获取自定义的Servie
- 新的旅程:NodeJS - 环境篇
- 关于cocos2dx的textfield事件响应
- Thinkphp 漏洞小试
- android sdk屏幕截图工具
- C语言超大数据相加计算整理
- Spring Boot 中使用 MyBatis 整合 Druid 多数据源
热门文章
- easyUI 展开DataGrid里面的行显示详细信息
- ubuntu - 安装hive
- Windows server iis部署Django详细操作
- ios中的奇怪崩溃Signal和EXC_BAD_ACCESS错误分析
- Java 大数相乘、大数相加、大数相减
- 解读人:林山云,Proteomic Identification of Protein Glutathionylation in Cardiomyocytes(心肌细胞蛋白质谷胱甘肽修饰的蛋白质组鉴定)
- P1452 Beauty Contest 旋转卡壳
- loj#6229. 这是一道简单的数学题 (??反演+杜教筛)
- 远程私有库的创建 pod 组件化
- anglarJs分页