03-树3 Tree Traversals Again
二叉树及其遍历
push为前序遍历序列,pop为中序遍历序列。将题目转化为已知前序、中序,求后序。
前序GLR 中序LGR
前序第一个为G,在中序中找到G,左边为左子树L,右边为右子树R。
将左右子树看成新的树,同理。
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
#include <iostream>
#include <cstdio>
#include <stack>
#include <string>
using namespace std; #define MaxSize 30 #define OK 1
#define ERROR 0 int preOrder[MaxSize];
int inOrder[MaxSize];
int postOrder[MaxSize]; void postorderTraversal(int preNo, int inNo, int postNo, int N); int main()
{
stack<int> stack;
int N; //树的结点数
cin >> N;
string str;
int data;
int preNo = , inNo = , postNo = ;
for(int i = ; i < N * ; i++) { //push + pop = N*2
cin >> str;
if(str == "Push") { //push为前序序列
cin >> data;
preOrder[preNo++] = data;
stack.push(data);
}else{ //pop出的是中序序列
inOrder[inNo++] = stack.top();
stack.pop(); //pop() 移除栈顶元素(不会返回栈顶元素的值)
}
}
postorderTraversal(, , , N);
for(int i = ; i < N; i++) { //输出后序遍历序列
if(i == ) //控制输出格式
printf("%d",postOrder[i]);
else
printf(" %d",postOrder[i]);
}
printf("\n");
return ;
} void postorderTraversal(int preNo, int inNo, int postNo, int N)
{
if(N == )
return;
if(N == ) {
postOrder[postNo] = preOrder[preNo];
return;
}
int L, R;
int root = preOrder[preNo]; //先序遍历GLR第一个为根
postOrder[postNo + N -] = root; //后序遍历LRG最后一个为根
for(int i = ; i < N; i++) {
if(inOrder[inNo + i] == root) { //找到中序的根 左边为左子树 右边为右子树
L = i; //左子树的结点数
break;
}
}
R = N - L - ; //右子树的结点数
postorderTraversal(preNo + , inNo, postNo, L); //同理,将左子树看成新的树
postorderTraversal(preNo + L + , inNo + L + , postNo + L, R);//同理,右子树
}
03-树3 Tree Traversals Again的更多相关文章
- PAT-1086(Tree Traversals Again)Java语言实现+根据中序和前序遍历构建树并且给出后序遍历序列
Tree Traversals Again Tree Traversals Again 这里的第一个tip就是注意到非递归中序遍历的过程中,进栈的顺序恰好是前序遍历的顺序,而出栈的顺序恰好是中序遍历的 ...
- HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- Tree Traversals
Tree Traversals 原题链接 常见的二叉树遍历的题目,根据后序遍历和中序遍历求层次遍历. 通过后序遍历和中序遍历建立起一棵二叉树,然后层序遍历一下,主要难点在于树的建立,通过中序遍历和后序 ...
- 树-伸展树(Splay Tree)
伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...
- HDU1710Binary Tree Traversals
HDU1710Binary Tree Traversals 题目大意:给一个树的前序遍历和中序遍历,要求输出后序遍历. (半年前做这道题做了两天没看懂,今天学了二叉树,回来AC了^ ^) 首先介绍一下 ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- PAT1086:Tree Traversals Again
1086. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue ...
- Binary Tree Traversals(HDU1710)二叉树的简单应用
Binary Tree Traversals Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- 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
PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...
随机推荐
- Sqool与kettle开源的ETL工具
现在的ETL都是基于管道的模式(数据流)运行,比较有名的有 TaskCTL ========================================== 数据抽取的开源工具 一个是RDBMS ...
- Linux平台块设备到字符设备(裸设备)的三种映射方式(转载)
在Linux平台oracle rac的组建过程中,如果使用ASM+RAW的存储方式的话,由于asm不支持块设备,支持持字符访问设备,所以需要配置将Block Device Drive转变成Charac ...
- Xfce 快捷键
1:显示桌面 修改文件:~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xmlvim命令:%s/"&l ...
- 反射IsGenericType
var propertyType = propertyInfo.PropertyType; if (propertyType.IsGenericType && propertyType ...
- 你不知道的JavaScript 二
词法作用域 上次说到作用域,将其定义为一套规则,这套规则用来管理引擎如何在当前作用 域以及嵌套的子作用域中根据标识符名称进行变量查找. 作用域共有两种主要的工作模型.第一种是最为普遍的,被大多数编程语 ...
- Atom 编辑器插件:amWiki 轻文库
amWiki 是一款基于 Javascript 脚本语言.依赖 Atom 编辑器.使用 Markdown 标记语法的轻量级开源 wiki 文库系统. amWiki 致力于让大家可以更简单.更便捷的建设 ...
- c-windows-1
< Back 我使用的是<windows程序设计>和VS 首先看到的第一个代码是: /*----------------------------------------------- ...
- 慕课网-安卓工程师初养成-1-1 Java简介
来源 http://www.imooc.com/video/1430 主要内容 Java平台应用 核心概念:JVM,JDK,JRE 搭建Java开发环境 使用工具开发安卓程序 经验技巧分享 Java历 ...
- redis学习(一)Redis应用场景
Redis开创了一种新的数据存储思路,使用Redis,我们不用在面对功能单调的数据库时,把精力放在如何把大象放进冰箱这样的问题上,而是利用Redis灵活多变的数据结构和数据操作,为不同的大象构建不同的 ...
- iOS取证将如漫漫长夜
日前因恐攻一案,FBI对一支已上锁的iPhone 5c束手无策,美国法院出具命令要求苹果配合,但被苹果公司执行长库克以维护安全及隐私为由悍然拒绝. 平心而论,各有其立场,但在一个犯罪案件之中,真的可以 ...