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: ...
随机推荐
- jquery、js获取table,遍历输出tr中各个td的内容。
首先,依赖jquery.. $('#btntb').click(function(){ $('#tab tr').each(function(i){ // 遍历 tr $(this).children ...
- 【BZOJ】【2946】【POI2000】公共串
后缀数组 好感动,复习了下后缀数组居然写出来了……(感谢ykz大神) 求最长公共子串……WA了一发是因为:[不同字符串之间要用不同的特殊字符隔开]否则就会匹配到相同→_→比如都是aaa结尾,如果用相同 ...
- iOS:制作一个简易的计算器
初步接触视图,制作了一个简易的计算器,基本上简单的计算是没有问题的,不是很完美,可能还有一些bug,再接再厉. // // ViewController.m // 计算器 // // Created ...
- 如何使用Windows Library文件进行持久化
前言 想象一下,假设在你不知道的情况下,攻击者在你的计算机上放置了一个恶意文件.每当你访问桌面上某个文件夹时(例如Documents文件夹),都会执行一次该文件.这样的场景,通过利用一种鲜为人知的持久 ...
- Mysql之sql语句操作
一.数据库级别操作 1.显示数据库 1 SHOW DATABASES; 默认数据库: mysql - 用户权限相关数据 test - 用于用户测试数据 information_schema - MyS ...
- Command 命令模式 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- tomcat 用AXIS2发布WebService 网站的方法
Axis2+tomcat7.0 实现webService 服务端发布与客户端的调用. Aixs2开发webService的方法有很多,在此只介绍一种比较简单的实现方法. 第一步:首先要下载开发所需要的 ...
- CSS 过滤器 兼容ie,火狐和谷歌
这篇汇总主要是提供一些CSS不透明的详细介绍,代码示例和解释,以实现这项有用的CSS技术在您的项目中兼容所有浏览器. 关于CSS 透明度,有一点需要注意的是,它虽然使用了很多年,但它一直以来都不是一个 ...
- UML解惑:图说UML中的六大关系
UML定义的关系主要有六种:依赖.类属.关联.实现.聚合和组合.这些类间关系的理解和使用是掌握和应用UML的关键,而也就是这几种关系,往往会让初学者迷惑.这里给出这六种主要UML关系的说明和类图描述, ...
- 正则 js分转元带千分符号
可以通过缩放来进行分到元的转换,同时使用正则对处理后的数字进行千分位格式化 方法1:(不丢失精度) function Fen2Yuan( num ) { if ( typeof num !== &qu ...