1086. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 ;
}

最新文章

  1. PHP缓存技术
  2. block fomating context
  3. 【经验记录】Jconsole Jvisualvm 监控Tomcat
  4. Swift3.0语言教程获取字符
  5. __declspec关键字详细用法
  6. CentOS学习笔记&mdash;软件管理程序RPM、YUM
  7. JavaScript中的事件冒泡机制
  8. GMT 绘制台站分布图
  9. Scikit-Learn与决策树
  10. 【BZOJ3884】上帝与集合的正确用法(欧拉定理,数论)
  11. struts2第一章-基本用法
  12. https协议的简单理解
  13. python 之 知识点(1)
  14. day05 Spring中自定义注解的用处-之获取自定义的Servie
  15. 新的旅程:NodeJS - 环境篇
  16. 关于cocos2dx的textfield事件响应
  17. Thinkphp 漏洞小试
  18. android sdk屏幕截图工具
  19. C语言超大数据相加计算整理
  20. Spring Boot 中使用 MyBatis 整合 Druid 多数据源

热门文章

  1. easyUI 展开DataGrid里面的行显示详细信息
  2. ubuntu - 安装hive
  3. Windows server iis部署Django详细操作
  4. ios中的奇怪崩溃Signal和EXC_BAD_ACCESS错误分析
  5. Java 大数相乘、大数相加、大数相减
  6. 解读人:林山云,Proteomic Identification of Protein Glutathionylation in Cardiomyocytes(心肌细胞蛋白质谷胱甘肽修饰的蛋白质组鉴定)
  7. P1452 Beauty Contest 旋转卡壳
  8. loj#6229. 这是一道简单的数学题 (??反演+杜教筛)
  9. 远程私有库的创建 pod 组件化
  10. anglarJs分页