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 ;
}
PAT 1086 Tree Traversals Again的更多相关文章
- PAT 1086 Tree Traversals Again[中序转后序][难]
1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...
- PAT 1020. Tree Traversals
PAT 1020. Tree Traversals Suppose that all the keys in a binary tree are distinct positive integers. ...
- PAT 1020 Tree Traversals[二叉树遍历]
1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...
- PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024 An inorder binary tree ...
- PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]
题目 An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For exam ...
- PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
随机推荐
- python opencv 人脸识别
def findface(image): import cv2 frame=cv2.imread('n1.jpg') classifier=cv2.CascadeClassifier("h ...
- 开篇-QT完全手册
嵌入式工具Qt的安装与使用 摘要 Qt是Trolltech公司的一个产品.Trolltech是挪威的一家软件公司,主要开 发两种产品:一种是跨平台应用程序界面框架:另外一种就是提供给做嵌入式Linux ...
- ldap客户端以及jenkins的配置
1.http://www.ldapbrowsermac.com/ 分免费版和收费版,使用免费版即可.请使用正确的协议,不然,中文会乱码:支持两种LDAP v3和LDAP v2 2.https://di ...
- TRIZ理论的进化法则分析(TRIZ学习笔记)
人们在创新和完好系统的过程能够遵循一定的规律(或者叫法则).从而降低创新和完好系统过程中的试错成本,以下就TRIZ的八大进化原则来进行说明(这个八大法则是前人们的总结,我这里当然会增加我的理解). 我 ...
- poj 食物链
比基础的并查集有些进步. 在以下这个链接中有详解: http://blog.csdn.net/ditian1027/article/details/20804911 对于每两个动物的关系,都是先推与终 ...
- C#的几种写文件方法
C#写文件处理操作在很多的开发项目中都会涉及,那么具体的实现方法是什么呢?这里向大家介绍三大方法,希望对你在开发应用中有所启发. 首先C#写文件处理操作必须先导入命名空间:using System.I ...
- Charles Proxy for Mac & Windows (4.1.3)破解激活工具
1.简介 2017年7月10日更新本博客,Charles已经更新到了4.1.3版本,并对应地给出破解jar包. Charles是一个Mac和Windows平台都可以使用的抓包工具,它的破解激活非常简单 ...
- go语言知识点
1.make()只是用3种内建的引用类型:切片.map和channel.new函数分配内存,make函数初始化. 2.:=只能使用在函数内部.
- MySql的入侵测试以及防范
在做了之前的SQL SERVER之后,便很想尝试一下MYSQL的入侵测试已经防范,与大家一起分享. 总的来说,我一直在用的是MYSQL,对MYSQL比较熟悉,相比较而言,感觉MYSQL更安全,这只是我 ...
- myeclipse创建maven android项目
一.搭建环境 1.安装android maven插件,我在网上找了半天.没有找到这个插件,于是选择了在线安装.选择myeclipse 的 [help]->[install form catalo ...