1086. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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的更多相关文章

  1. pat1086. Tree Traversals Again (25)

    1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...

  2. PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列

    Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...

  3. Tree Traversals

    Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...

  4. HDU 1710 二叉树的遍历 Binary Tree Traversals

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  5. hdu1710(Binary Tree Traversals)(二叉树遍历)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  6. HDU1710Binary Tree Traversals

    HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...

  7. HDU-1701 Binary Tree Traversals

    http://acm.hdu.edu.cn/showproblem.php?pid=1710 已知先序和中序遍历,求后序遍历二叉树. 思路:先递归建树的过程,后后序遍历. Binary Tree Tr ...

  8. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  9. HDU 1710-Binary Tree Traversals(二进制重建)

    Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

随机推荐

  1. Java-HttpServlet

    /** * * Provides an abstract class to be subclassed to create * an HTTP servlet suitable for a Web s ...

  2. MTK GPIO 一些理解

    基于6575平台: GPIO驱动程序提供了两个接口: (1)内核空间:所提供的GPIO驱动程序,驱动程序可以调用其他函数 (2)用户空间:用户空间的程序可以通过发送IOCTL   /dev/ mtgp ...

  3. navicat里导入和导出.sql文件

    一:Navicat导入MySQL的.sql文件. 打开连接connection,选择你要导入的数据库,双击打开(或者新建一个),然后右键该数据库,选"运行批次任务文件(execute sql ...

  4. nasm预处理器(3)

    nasm提供一个限定符.nolist,可以包含它到一个宏定义中,这样该宏就不会在列表文件中被展开:限定符 .nolist直接放到参数后面: %macro foo 1.nolist 条件汇编 和C预处理 ...

  5. obj-c编程03:多个参数方法的定义

    好吧,虽说本猫不能自吹精通十几种语言,但是也见过十几种语言的语法啊.像obj-c这样奇葩,或者说另类的写法还是头一次见到,完整写法我都不知道怎么起方法名了.虽说有简短写法,可和C比起来那个" ...

  6. day07_Tomcat服务器与http学习笔记

    ============================================================ 一.Tomcat服务器(很熟悉) 1.Web开发概述 WEB,在英语中web即 ...

  7. 织云 Metis:看腾讯怎么做智能运维

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作为企业智能运维门户,业界早已关注织云的智能运维体系.我们很荣幸地宣布织云 Metis 智能运维体系正式发布.自此,织云家族已发布:织云企业 ...

  8. python 网络框架twisted基础学习及详细讲解

    twisted网络框架的三个基础模块:Protocol, ProtocolFactory, Transport.这三个模块是构成twisted服务器端与客户端程序的基本.Protocol:Protoc ...

  9. java的finalize方法使用

    1. finalize的作用 finalize()是Object的protected方法,子类可以覆盖该方法以实现资源清理工作,GC在回收对象之前调用该方法. finalize()与C++中的析构函数 ...

  10. (function(){xxx})(); 写法解释

    常见格式:(function() { /* code */ })(); 解释:包围函数(function(){})的第一对括号向脚本返回未命名的函数,随后一对空括号立即执行返回的未命名函数,括号内为匿 ...