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 ...
随机推荐
- unity 球体表面平均分割点
之前看了别人的一份源码,讲到了球体表面平均分割点,于是也好奇去查了一下算法,自己写不出来,借用算法在unity写了一个小demo using UnityEngine; using System.Col ...
- cocos2d 从v1.x升级到v2.x需要注意的几个地方
首先v1.x一些CCNode定位函数实现的有问题,导致返回的CCPoint的x坐标不正确(超出320后无变化),怀疑是其对屏幕旋转判断的不正确;而且这种现象在iOS 7.1之前的模拟器中运行都正常,在 ...
- 集群RPC通信
RPC即远程过程调用,它的提出旨在消除通信细节.屏蔽繁杂且易错的底层网络通信操作,像调用本地服务一般地调用远程服务,让业务开发者更多关注业务开发而不必考虑网络.硬件.系统的异构复杂环境. 先看看集群中 ...
- MySQL数据库存储过程动态表建立(PREPARE)
PREPARE statement_name FROM sql_text /*定义*/ EXECUTE statement_name [USING variable [,variable...]] / ...
- ASP.NET Core 2.0 使用NLog实现日志记录
1.安装NuGet包 1.Install-Package NLog.Web.AspNetCore 2.Install-Package NLog 在csproj中编辑: <PackageRefer ...
- IT轮子系列(一)——DropDownList 的绑定,你秒懂了吗
前言 最近猛然惊觉(说是猛然,是因为自己工作那么多年,居然不自知.不反省),在开发中,自己碰到一些常用的功能代码块,还是习惯性的baidu,然后copy....这样的操作,不知自己重复了多少遍.现在回 ...
- Java Web开发中路径问题小结
Java Web开发中,路径问题是个挺麻烦的问题,本文小结了几个常见的路径问题,希望能对各位读者有所帮助. (1) Web开发中路径的几个基本概念 假设在浏览器中访问了如下的页面,如图1所示: 图1 ...
- OSGI介绍
OSGI介绍 OSGI简介 OSGI (Open Service Gateway Initiative)联盟成立于1999 年,它是一个非盈利的国际组织,旨在建立一个开放的服务规范,为通过网络向设备提 ...
- iframe中 父页面和子页面查找元素的方法
从父页面中查找iframe子页面中对象的方法:JS: document.getElementById('iframe').contentWindow //查找iframe加载的页面的window对象 ...
- 【转】H.264RTP封包原理
原文地址:H.264RTP封包原理 作者:cnp11 1. 引言 随着信息产业的发展,人们对信息资源的要求已经逐渐由文字和图片过渡到音频和视频,并越来越强调获取资源的实时性和互动性.但人们又面 ...