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 ...
随机推荐
- 基于ARM_contexA9 led驱动编程
关于友善之臂出的这款contexA9开发板,目前在网络上的资源较少,特别是内核的,非常之少,鉴于这种情况,我将会写一个系列的驱动来做关于tiny4412这款板子开发的总结. 简单介绍一下: Tiny4 ...
- android 数据重构(仿淘宝浏览记录,足迹)
数据结构 ->数据重构 原因 处理这个数据的主要原因是,后台服务器返回的数据格式在ios那边因为其控件可以对数据进行分区显示,可以直接处理,而在android上我们显示控件就是listview, ...
- 关于C++“加、减机制”的整理
今天上C++的课,杨老师提到C++继承是“加机制”的,而没有像人类进化一样采取的是“减机制”,这样会导致代码的膨胀和冗余.回来后,特地查阅了一下资料,发现这方面的文章很少. 下边的资料摘自网上及杨老师 ...
- MTU 最大传输单位
MTU 最大传输单位 通过上面 MAC 封装的定义,现在我们知道标准以太网络frame所能传送的数据量最大可以到达 1500 bytes , 这个数值就被我们称为 MTU (Maximum Trans ...
- DBUS基础知识
转:http://www.cnblogs.com/wzh206/archive/2010/05/13/1734901.html DBUS基础知识 1. 进程间使用D-Bus通信 D-Bus是一种高级 ...
- C语言之实现随机数产生算法
随机数,也就是在不同的时刻产生不同的数值.在UNIX操作系统和window的操作系统上,我们知道有一个函数rand,它就是用来产生随机数的函数API接口,那么它的原理如何实现? 如果约定a1=f(se ...
- CoordinatorLayout
CoordinatorLayout作为"super-powered FrameLayout"基本实现两个功能: 1.作为顶层布局 2.调度协调子布局 CoordinatorLa ...
- Robot Framework + Pywinauto 框架实现Windows GUI Automation
Robot Framework is a generic test automation framework for acceptance testing and acceptance test-dr ...
- [51nod 1515] 明辨是非
Description 给\(n\)组操作,每组操作形式为\(x\;y\;p\). 当\(p\)为\(1\)时,如果第\(x\)变量和第\(y\)个变量可以相等,则输出\(YES\),并限制他们相等: ...
- Day8 面向对象反射 item方法 打印对象信息__str__ 构析方法__del__ 程序的异常处理
反射:通过字符串来访问到所对应的值(反射到真实的属性上). eg: class Foo: x=1 def __init__(self,name): self.name=name def f1(self ...