题目的大意是:进行一系列的操作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. HDU 1165 公式推导题

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=1165 Eddy's research II Time Limit: 4000/2000 MS (Java/Othe ...

  2. FD.io社区中国行暨未来网络技术沙龙·南京站 会议小结

    What is FD.io VPP? FD.io VPP(Fast Data Input/Output Vector Packet Processing)is a new network multi- ...

  3. Vue学习—Vue写一个图片轮播组件

    1.先看效果: 熟悉的图片轮播,只要是个网站,百分之90以上会有个图片轮播.我认为使用图片轮播. 第一可以给人以一种美观的感受,而不会显得网站那么呆板, 第二可以增加显示内容,同样的区域可以显示更多内 ...

  4. 硬盘分区表知识——详解硬盘MBR

    这片文章说得很详细,原文:http://hi.baidu.com/waybq/blog/item/3b8db64bef3dc7f583025c66.html --------------------- ...

  5. MySql多表关联,根据某列取前N条记录问题

    近来遇到一个问题:“MySql多表关联,根据某列取前N条记录”. 刚开始一直在想,SQL语句是否可以做到直接查询出来,但几经折磨,还是没能写出SQL语句,-------如果有大牛的话,望指点迷津.我把 ...

  6. MYSQL 入门全套

    MySQL简介 1.什么是数据库 ? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,它产生于距今六十多年前,随着信息技术和市场的发展,特别是二十世纪九十年代以后,数据管理不再仅 ...

  7. docker入门——安装(CentOS)、镜像、容器

    Docker简介 什么是docker 官方解释: Docker is the company driving the container movement and the only container ...

  8. urllib库使用方法 3 get html

    import urllib.requestimport urllib.parse #https://www.baidu.com/s?ie=UTF-8&wd=中国#将上面的中国部分内容,可以动态 ...

  9. 修改并编译golang源码

    最近为了做Hyperledger Fabric国密改造,涉及到了golang源码的改动.特将操作过程整理如下,以供参考: golang的源码安装其实比较简单,只需运行源码包中的脚本src/all.ba ...

  10. Go语言反射之反射调用

    ## 1 概述利用反射,不仅可以获取信息,还可以创建实例,执行函数和方法.就是反射代理执行. <!-- more -->## 2 创建实例创建实例的前提是具有 `reflect.Type` ...