PAT1086:Tree Traversals Again
1086. Tree Traversals Again (25)
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 思路
在1020的基础上稍微增加了难度。
1.仔细观察发现这道题中栈的压入操作其实是树的前序遍历,弹出操作是树的中序遍历
2.那么可以根据输入操作的不同构造出树的前序序列和中序序列,由此转化为类似pat1020 的问题了。
代码
#include<iostream>
#include<vector>
using namespace std;
vector<int> preorder();
vector<int> inorder();
vector<int> mypostorder();
int index = ; void postorder(int pfirst,int plast,int ifirst,int ilast )
{
if(pfirst > plast || ifirst > ilast)
return;
int i = ;
while(preorder[pfirst] != inorder[ifirst + i]) i++; //find root's index in inorder sequence postorder(pfirst + ,pfirst + i,ifirst,ifirst + i);
postorder(pfirst + i + ,plast,ifirst + i + ,ilast);
mypostorder[index++] = preorder[pfirst];
} int main()
{
int N;
while(cin >> N)
{
vector<int> stk;
int in = ,out = ;
//input
while(in <= N || out <= N)
{
string command;
int value;
cin >> command ;
if(command[] == 'u')
{
cin >> value;
preorder[in++] = value;
stk.push_back(value);
}
else
{
inorder[out++] = stk.back();
stk.pop_back();
}
} //handle
postorder(,N ,,N);
//output
int j = ;
cout << mypostorder[j];
for(int j = ;j <= N;j++)
cout <<" " << mypostorder[j];
cout << endl;
}
}
PAT1086:Tree Traversals Again的更多相关文章
- pat1086. Tree Traversals Again (25)
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列
Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...
- Tree Traversals
Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...
- HDU 1710 二叉树的遍历 Binary Tree Traversals
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu1710(Binary Tree Traversals)(二叉树遍历)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- HDU1710Binary Tree Traversals
HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...
- HDU-1701 Binary Tree Traversals
http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- HDU 1710-Binary Tree Traversals(二进制重建)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
随机推荐
- Weka 算法大全
关联规则挖掘 (一) Apriori (二) FilteredAssociator (三) FPGrowth (四) GeneralizedSequentislPatterns (五) Pr ...
- LIRe 源代码分析 7:算法类[以颜色布局为例]
===================================================== LIRe源代码分析系列文章列表: LIRe 源代码分析 1:整体结构 LIRe 源代码分析 ...
- 如何配置android的adb环境变量
如果打开DOS窗口,输入adb显示既不是内部命令也不是外部命令,则说明没有配置adb环境变量.方法如下: 第一步: 打开环境变量配置窗口.右击计算机,属性-高级系统设置-环境变量. 第二部: 添加an ...
- OAF更改动态头行
选择头信息,动态刷新行信息.本文将详细介绍该种需求的做法. 本例沿用<OAF-头行结构>的am与vo,所以在进行本例之前,请先完成<OAF-头行结构> 一.创建页面 在test ...
- 用xml来编写动画
我们可以使用代码来编写所有的动画功能,这也是最常用的一种做法.不过,过去的补间动画除了使用代码编写之外也是可以使用XML编写的,因此属性动画也提供了这一功能,即通过XML来完成和代码一样的属性动画功能 ...
- 【Android 应用开发】BluetoothAdapter解析
这篇文章将会详细解析BluetoothAdapter的详细api, 包括隐藏方法, 每个常量含义. 一 BluetoothAdapter简介 1.继承关系 该类仅继承了Object类; 2.该类作用 ...
- IOS动画(Core Animation)总结 (参考多方文章)
一.简介 iOS 动画主要是指Core Animation框架.官方使用文档地址为:Core Animation Guide. Core Animation是IOS和OS X平台上负责图形渲染与动画的 ...
- Java 去掉字符串中的换行符回车符等
去掉一个字符串中的换行符.回车符等,将连续多个空格替换成一个空格 String string = "this just a test" Pattern p = Pattern.co ...
- Best Time to Buy and Sell Stock i
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- Django signals机制的几个简单问题
1.Django signals机制不是异步执行,是同步执行,所以需要异步执行的耗时任务不能用这个. 2.异步耗时任务不用这个,那些用signals?主要是解耦那些多次重复场合被调用的函数.直接用事件 ...