题目的大意是:进行一系列的操作push,pop。来确定后序遍历的顺序

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.

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 t

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
思路:push过程就是前序遍历,pop过程就是中序遍历,再写一个根据前序和中序遍历确定后序遍历的算法。
 2.取先序序列中的第一个元素,该元素为根结点
    3.根据根结点在中序序列中查找根结点的位置,从而得到该树左子树结点个数(L)与右子树的结点个数(R)
    4.在后序序列数组中,第0到第L个元素为左子树,第L+1到第L+R个元素为右子树,最后一个元素为根结点
 
 
#include<cstdio>
#include<stack>
#include<iostream>
#include<string>
using namespace std; #define MAX 30
int preOrder[MAX];
int inOrder[MAX];
int postOrder[MAX]; //根据前序和中序划分,来确定后序遍历。前序的第一个数字为根结点,
//找到根结点root在中序数组位置,中序数组中root左边为根结点左子树,右边为右子树
void Solve(int preL,int inL,int postL,int n){
if(n==)return;
if(n==){
postOrder[postL]=preOrder[preL];
}
int root=preOrder[preL];
postOrder[postL+n-]=root;
int i,R,L;
for(i=;i<n;i++){
if(root==inOrder[inL+i])break;
}
L=i,R=n-i-; //L为左子树结点数目,R为右子树结点数目
Solve(preL+,inL,postL,L); //确定后序数组中根结点root左边的排列顺序
Solve(preL+L+,inL+L+,postL+L,R);
} int main(){
int n;
for(int i=;i<MAX;i++){
preOrder[i]=;
inOrder[i]=;
postOrder[i]=;
}
stack<int> s;
cin>>n;
string str;
int data;
int index=,pos=;
for(int i=;i<*n;i++){
cin>>str;
if(str=="Push"){ //push代表前序遍历
cin>>data;
s.push(data);
preOrder[index++]=data;
}else if(str=="Pop"){ //pop为中序遍历
inOrder[pos++]=s.top();
s.pop();
}
}
Solve(,,,n);
for(int i=;i<n;i++){
if(i>)printf(" ");
printf("%d",postOrder[i]);
}
return ;
}

Tree Traversals Again(根据前序,中序,确定后序顺序)的更多相关文章

  1. HDU 1710Binary Tree Traversals(已知前序中序,求后序的二叉树遍历)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1710 解题思路:可以由先序和中序的性质得到 : 先序的第一个借点肯定是当前子树的根结点, 那么在 中序 ...

  2. hdu 1701 (Binary Tree Traversals)(二叉树前序中序推后序)

                                                                                Binary Tree Traversals T ...

  3. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

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

  4. PAT 1086 Tree Traversals Again[中序转后序][难]

    1086 Tree Traversals Again(25 分) An inorder binary tree traversal can be implemented in a non-recurs ...

  5. 【二叉树遍历模版】前序遍历&&中序遍历&&后序遍历&&层次遍历&&Root->Right->Left遍历

    [二叉树遍历模版]前序遍历     1.递归实现 test.cpp: 12345678910111213141516171819202122232425262728293031323334353637 ...

  6. Java实现二叉树的前序、中序、后序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的三种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似,也简单 ...

  7. LeetCode二叉树的前序、中序、后序遍历(递归实现)

    本文用递归算法实现二叉树的前序.中序和后序遍历,提供Java版的基本模板,在模板上稍作修改,即可解决LeetCode144. Binary Tree Preorder Traversal(二叉树前序遍 ...

  8. Java实现二叉树的前序、中序、后序、层序遍历(非递归方法)

      在上一篇博客中,实现了Java中二叉树的四种遍历方式的递归实现,接下来,在此实现Java中非递归实现二叉树的前序.中序.后序.层序遍历,在非递归实现中,借助了栈来帮助实现遍历.前序和中序比较类似, ...

  9. PHP递归方法实现前序、中序、后序遍历二叉树

    二叉树是每个节点最多有两个子树的树结构.通常子树被称作“左子树”(left subtree)和“右子树”(right subtree). class Node { public $value; pub ...

  10. 二叉树各种相关操作(建立二叉树、前序、中序、后序、求二叉树的深度、查找二叉树节点,层次遍历二叉树等)(C语言版)

    将二叉树相关的操作集中在一个实例里,有助于理解有关二叉树的相关操作: 1.定义树的结构体: typedef struct TreeNode{ int data; struct TreeNode *le ...

随机推荐

  1. python接口自动化读取json,yaml配置文件+封装requests+unittest+HTMLRunner实现全自动化

    # coding=utf-8 import json import requests class TestApi(object): """ /* @param: @ses ...

  2. [LuoguP1221]最多因子数

    [Luogu1221]最多因子数(Link) 求区间[L,R]内约数个数最多的数和它的约数个数. 这个题吧,乍一看确实不是很难,然后稍微一想,嗯,是个傻*题.这是唯一感受,不要问我为什么. 首先我们定 ...

  3. Jmeter--thrift接口压测,调用jar包失败报错:java.lang.NoSuchMethodError:

    调用thrift接口压测的jar包,出现了错误:java.lang.NoSuchMethodError: 错误可能的原因: 有这个类,该类真的没有这个方法 有这个类,而且有好几个,他们之间发生了冲突 ...

  4. HDU 1208 Pascal's Travels 经典 跳格子的方案数 (dp或者记忆化搜索)

    Pascal's Travels Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Su ...

  5. 在 S5PV210 的 开发板上 点亮 一个 LED 灯

    参考学习教程:周立功嵌入式Linux开发教程-(上册) 材料:首先 准备一个 安装好 Linux 的 开发板 使用  xshell 工具 连接 开发板  ,winscp 工具 连接 开发板  ,  准 ...

  6. Template Method(模板方法)模式

    1.概述 在面向对象开发过程中,通常我们会遇到这样的一个问题:我们知道一个算法所需的关键步骤,并确定了这些步骤的执行顺序.但是某些步骤的具体实现是未知的,或者说某些步骤的实现与具体的环境相关.例子1: ...

  7. [转载]单点登录SSO:概述与示例

    原文地址: https://www.cnblogs.com/baibaomen/p/sso.html 目录 概述 演示一:零改造实施单点登录 演示二: 单点注销 演示三:集成AD认证 演示四:客户端单 ...

  8. [已解决] 设置无效字段为-1 时,引发的 DataGridView DataError

    由于问题一句话说不清.所以标题里也没写明白.大概情况是这样.我一直使用dotNetBar控件来做UI,其中的DataGridView很常用.过去一直有发现DataError的错误,通过截取消息暂时屏蔽 ...

  9. React学习(一)

    一. 允许HTML和JavaScript代码混写,使用JSX语法:遇到HTML标签就用HTML规则解析,遇到{}的代码块就用js解析 var names = ['Alice', 'Emily', 'K ...

  10. Hive(10)-文件存储格式

    Hive支持的存储数据的格式主要有:TEXTFILE .SEQUENCEFILE.ORC.PARQUET 一. 列式存储和行式存储 左边为逻辑表,右边第一个为行式存储,第二个为列式存储 1. 行式存储 ...