题目的大意是:进行一系列的操作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. 解密蓝牙mesh系列

    解密蓝牙mesh系列 https://mp.weixin.qq.com/s/KdVhkgcmHIboA0xPFqFCgQ 1.NRF52832 & NRF52840 BLE mesh 协议栈 ...

  2. NOIP Day1总结

    Day1T1玄学考试 在开始之前,我犯了考前综合症,各种不安各种焦躁. 结果当我去到考场的时候,看了T1...... T1:road 这不是裸的原题么这!我当时心里瞬间想到积木大赛.这明显就是积木大赛 ...

  3. luogu11月月赛T3咕咕咕(组合数学)

    题目描述 小 F 是一个能鸽善鹉的同学,他经常把事情拖到最后一天才去做,导致他的某些日子总是非常匆忙. 比如,时间回溯到了 2018 年 11 月 3 日.小 F 望着自己的任务清单: 看 iG 夺冠 ...

  4. 浅谈nodejs中HTTP模块应用

    这里给大家分享下后端人员如果利用nodejs对数据的一些处理情况  适用于初学者使用 大牛勿喷 给大家分享下主要后端思想部分代码,前端部分就不展示了 const http = require(&quo ...

  5. node创建服务器

    //引入核心模块 const http = require('http'); //创建服务器 http.createServer((req,res)=>{ }).listen(3000); // ...

  6. 树莓派 ubuntu16.04 安装SSH 配置SSH 开机自启SSH

    入手个树莓派3B 装了 ubuntu 16.04 需要用到SSH 记录下 0.先获得树莓派IP 树莓派 使用网线连接路由器和树莓派 在路由器设置页面(一般是192.168.1.1具体看路由器的型号和设 ...

  7. 基于OpenCV的微信跳一跳外挂

    摘要:微信跳一跳是时下热门的微信小游戏,基本原理是根据按压屏幕的时间控制棋子跳过的距离,使其跳到下一个方块上:现利用Android adb工具,PC端获取实时截图,使用OpenCV库分析图片计算距离, ...

  8. 双端队列 ADT接口 数组实现

    Deque ADT接口 DEQUEUE.h: #include <stdlib.h> #include "Item.h" void DEQUEUEinit(int); ...

  9. Python使用__slots__限制实例属性

    #定义一个类Student class Student(object): __slots__ = ('name','age') #用元组(tuple)的形式绑定属性名称 s = Student() s ...

  10. leetcode-746-Min Cost Climbing Stairs(动态规划)

    题目描述: On a staircase, the i-th step has some non-negative cost cost[i] assigned (0 indexed). Once yo ...