二叉树及其遍历

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

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

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

  2. HDU 1710 Binary Tree Traversals(树的建立,前序中序后序)

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

  3. Tree Traversals

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

  4. 树-伸展树(Splay Tree)

    伸展树概念 伸展树(Splay Tree)是一种二叉排序树,它能在O(log n)内完成插入.查找和删除操作.它由Daniel Sleator和Robert Tarjan创造. (01) 伸展树属于二 ...

  5. HDU1710Binary Tree Traversals

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

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

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

  7. PAT1086:Tree Traversals Again

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

  8. Binary Tree Traversals(HDU1710)二叉树的简单应用

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

  9. PAT 1020 Tree Traversals[二叉树遍历]

    1020 Tree Traversals (25)(25 分) Suppose that all the keys in a binary tree are distinct positive int ...

  10. PAT 1086 Tree Traversals Again

    PAT 1086 Tree Traversals Again 题目: An inorder binary tree traversal can be implemented in a non-recu ...

随机推荐

  1. I/B/P SP/SI

    H264中I.B.P帧 I帧:只作为参考帧,采用帧内预测 B帧:以其前面的I帧或P帧和后面的P帧作为参考帧 P帧:只能以前面的I帧或P帧作为参考帧 H264中的SP SI SP和SI是H264中引入的 ...

  2. Grunt 之 watch 和 livereload

    现在 watch 中已经集成了 livereload ,所以把它们放在一起说明. watch 可以监控特定的文件,在添加文件.修改文件.或者删除文件的时候自动执行自定义的任务,比如 livereloa ...

  3. C++ 之旅:前言

    日前,拿起了C++教材开始学习. 在大学二年级的时候,其实C++已经是我们的必修课程.然而,那时的我刚从C语言的噩梦中逃出来,对C++也不甚喜爱.刚接触编程的我当时实在无法理解譬如下面这段 int x ...

  4. Android开发-API指南-常用Intent

    Common Intents 英文原文:http://developer.android.com/guide/components/intents-common.html 采集(更新)日期:2014- ...

  5. APP完整的启动流程

    0.加载+load方法 1.执行Main函数 2.执行UIApplicationMain函数. 3.创建UIApplication对象,并设置UIApplicationMain对象的代理.UIAppl ...

  6. 辅助的写与数据库交互的XML文件的类

    现在企业级WEB应用中与数据库交互的XML文件都是通过插件自动生成的,不过有些时候修改比较老的项目的时候也是需要手动的来做这一动作的!如下代码就是一个实现上述的功能的辅助类,在此记录一下以备后用! p ...

  7. Activity使用Dialog样式导致点击空白处自动关闭的问题

    将Activity设置成窗口的样式实现Dialog或者Popupwindow效果在开发中是很常用的一种方式,在AndroidMenifest.xml中将需要设置的Activity增加android:t ...

  8. 安全终止MFC线程

    终止线程 有两种情况可以使线程结束:控制函数结束或者根本就不允许线程完成,而提前终止它.我们可以想象在WORD中进行后台打印,如果打印结束了,那线程就可以结束了.如果用户中止了打印,那后台打印线程也要 ...

  9. ionic cordova plugin simple demo

    要用cordova plugin 的话还是需要设置一下的 1. 下载 ng-cordova.js download the zip file here 2. 在index.html 中引用 (cord ...

  10. vyos (一) 基础配置

    http://www.lowefamily.com.au/2015/11/29/using-a-vyos-router-with-hyper-v/1/ http://thomasvochten.com ...