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 ...
 
随机推荐
- Hibernate——主键配置
			
<id>元素中的<generator>用来为该持久化类的实例生成唯一的标识,hibernate提供了很多内置的实现. Increment:由hibernate自动递增生成标识符 ...
 - jmeter随笔(1)-在csv中数据为json格式的数据不完整
			
昨天同事在使用jmeter遇到问题,在csv中数据为json格式的数据,在jmeter中无法完整的取值,小怪我看了下,给出解决办法,其实很简单,我们一起看看,看完了记得分享给你的朋友. 问题现象: 1 ...
 - 关于NopCommerce3.6版的@Html.Widget(“home_page_top”)的说明
			
以首页幻灯片为例子,首页幻灯片是在插件Nop.Plugin.Widgets.NivoSlider里面实现的 首页视图位置 这里其实是加载插件里面的视图内容,具体实现在插件实现 这个是扩展方法,就是执行 ...
 - 慕课网-安卓工程师初养成-2-12 如何在Java中使用注释
			
来源:http://www.imooc.com/code/1274 在编写程序时,经常需要添加一些注释,用以描述某段代码的作用. 一般来说,对于一份规范的程序源代码而言,注释应该占到源代码的 1/3 ...
 - PostMessage与SendMessage的区别(二)
			
在做基于窗口的Windows程序的时候,我们避免不了要向窗口发送消息,有两种方式,一种是PostMessage,另外一种是SendMessage.关于这两个宏,我是通过狠狠的看MSDN才搞明白的,那里 ...
 - 吉布斯现象( Gibbs)
			
在连续傅里叶级数(或积分)变换中,信号所对应的离散频谱(或连续频谱)为(或),其频率是无限离散分布的(或频谱的分布范围是无限区间的).很显然,单位时间内,频率较低(简称低频,即较小)的简谐波相对频率较 ...
 - 为知笔记 Markdown 新手指南
			
为知笔记 Markdown 新手指南 http://www.wiz.cn/feature-markdown.html 时序图,流程图详细流程图语法 http://adrai.github.io/flo ...
 - linux取某个字段排重
			
排重统计 cat a.txt | awk -F ';' '{print $2}' | sort -u | wc -l
 - dwr与ssh框架整合教程
			
(1)dwr与ssh框架整合教程dwr框架介绍. DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开 发人员开发包含AJ ...
 - linux 关闭系统提示声音
			
关闭Linux 提示声音: rmmod pcspkr //永久关闭 在/etc/modprobe.d/blacklist文件最后加上 blacklist pcspkr