PAT 1086 Tree Traversals Again

题目:

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

地址:http://pat.zju.edu.cn/contests/pat-a-practise/1086

从非递归中序遍历中出栈与入栈的序列中找出这个棵树的后序遍历序列。很多人的做法是重新构造出这棵树,但我的方法不需要构造出一棵树,只需要利用两个栈,并且向后多看一个序列即可完成。首先,第一栈是用来模拟原来中序序列的出栈和入栈,第二栈是用来存储有右孩子的根节点的。整个算法就是在分析这个出栈入栈的序列,如果当前序列为入栈,则我们把对应的节点压入第一个栈中,如果当前为出栈序列,则可分为两种情况。第一,后面紧跟着是入栈操作,说明此时出栈的节点要往右孩子那边遍历,所有把该出栈的节点压入第二个栈,并且记下该节点的右孩子值;第二,后面紧跟着还是出栈操作或者为最后一个出栈操作,此时第一个栈出栈一个节点,如果第二个栈非空,并且第二个栈的栈顶节点的右孩子为第一个栈的出栈节点,那么输出右孩子节点,第二个栈出栈,注意这里是一个while循环,因为后序遍历有可能是顺着右节点往上输出的。代码:

 #include <string>
#include <vector>
#include <iostream>
#include <stack>
using namespace std; void print(int t, int &flag)
{
if(flag){
flag = ;
cout << t;
}else{
cout << ' ' << t;
}
} int main()
{
int n;
const string s1 = "Push";
const string s2 = "Pop";
while(cin >> n){
n <<= ;
vector<string> input(n,"");
vector<int> val(n,);
for(int i = ; i < n; ++i){
cin >> input[i];
if(input[i] == s1)
cin >> val[i];
}
stack<int> stk;
stack<int> root;
vector<int> rightchild(n>>,);
int flag = ;
for(int i = ; i < n; ++i){
if(input[i] == s1){
stk.push(val[i]);
}else{
if(i+ >= n || input[i+] == s2){
int t = stk.top();
stk.pop();
if(root.empty()){
print(t,flag);
}else{
while(!root.empty() && rightchild[root.top()] == t){
print(t,flag);
t = root.top();
root.pop();
}
print(t,flag);
}
}else{
int t = stk.top();
stk.pop();
root.push(t);
rightchild[t] = val[i+];
}
} }
cout << endl;
}
return ;
}

最新文章

  1. thinkphp3.2.3之自动完成的实现
  2. Mybatis架构学习
  3. ADB命令详解
  4. 在spring框架中配置Quartz定时器发生错误: class org.springframework.scheduling.quartz.JobDetailBean has interface org.quartz.JobDetail as sup
  5. php生成json或者xml数据
  6. 关于orapwd命令entries参数的探究
  7. Quartus II 12.0 下载、安装和破解
  8. Xcode7主题路径
  9. 【UVA1371】Period (二分+DP)
  10. python批量改动指定文件夹文件名称
  11. 无法打开登录所请求的数据库 &quot;ASPState&quot;。登录失败。 用户 &#39;NT AUTHORITY/SYSTEM&#39; 登录失败。
  12. IOS UI 第二篇:基本UI
  13. DOM4J使用简介
  14. c 指针函数 vs 函数指针
  15. flask 使用Flask-SQLAlchemy管理数据库(连接数据库服务器、定义数据库模型、创建库和表)
  16. 20165310 NetSec Week4 Exp2 后门原理与实践
  17. 【Python】文件读写操作
  18. DBCC--常用跟踪标记
  19. win7安装docker报错:error during connect: Get http ..... the system cannot find the file specified
  20. Vue脚手架搭建步骤

热门文章

  1. FizzBuzzWhizz问题python解法
  2. Kmeans算法原理极其opencv实现(转帖)
  3. Handler Thread 内部类引起内存泄露分析
  4. Volley框架的介绍使用
  5. Design Your Own Protocol In Five Minutes
  6. Web.config中加了system.diagnostics节点后就不能访问了
  7. UVa 10820 - Send a Table
  8. linux可视化桌面安装
  9. PHP高级教程-Error
  10. 【转】Spring中IoC的优点与缺点