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