题目

  • 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 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

分析思路

  • Push操作刚好是树的前序遍历过程;Pop操作刚好是树的中序遍历操作

AC代码

/*!* \file 03-树3 Tree Traversals Again.cpp** \author ranjiewen* \date 2017/04/25 23:51** */#include <iostream>
#include <cstdio>
#include <stack>
#include <string>using namespace std;#define MaxSive 30
#define OK 0
#define ERROR -1int preOrder[MaxSive];
int inOrder[MaxSive];
int postOrder[MaxSive];void postOrderTraversal(int preNum,int inNum,int postNum,int Num);int main()
{stack<int> s;int N; //树的结点数cin >> N;string str;int data;int preNum = 0, inNum = 0, postNum = 0;for (int i = 0; i < N * 2;i++)  //push+pop=N*2 {cin >> str;if (str=="Push") //Push为前序序列{cin >> data;preOrder[preNum++] = data;s.push(data);}else  //Pop为中序序列{inOrder[inNum++] = s.top();s.pop();  //移除栈顶元素(不会返回栈顶元素的值)}}postOrderTraversal(0,0,0,N); //每棵子树前,中,后序数组的开始下标,N为子树的结点个数for (int i = 0; i < N;i++)  //输出后序遍历序列{if (i==0)   //输出格式控制{printf("%d", postOrder[i]);}else{printf(" %d", postOrder[i]);}}printf("\n");return 0;
}void postOrderTraversal(int preNum, int inNum, int postNum, int Num)
{if (Num==0){return;}if (Num==1){postOrder[postNum] = preOrder[preNum];return;}int L=0, R=0; //递归左右子树的结点个数int root = preOrder[preNum]; //先序遍历的第一个节点为根节点postOrder[postNum + Num - 1] = root; //填后序遍历的坑for (int i = 0; i < Num;i++){if (inOrder[i+inNum] == root)//不要掉了preNum //在中序遍历中找到根节点,分为左右子树递归{L = i;  //左子树结点个数break;}}R = Num- L - 1; //右子树结点个数postOrderTraversal(preNum+1,inNum,postNum,L);postOrderTraversal(preNum + L + 1, inNum + L + 1, postNum + L, R); //右子树递归调用,注意开始下标}

Reference

题目来源

03-树3 Tree Traversals Again相关推荐

  1. 03-树3 Tree Traversals Again(树的遍历)

    03-树3 Tree Traversals Again 分数 25 作者 陈越 单位 浙江大学 An inorder binary tree traversal can be implemented ...

  2. PAT甲级1020 Tree Traversals:[C++题解]树的遍历、由中序序列和后序序列递归建树

    文章目录 题目分析 题目链接 题目分析 题意重述:给定一棵二叉树的后序遍历序列和中序遍历序列,让求层次遍历的序列. 分析: 后序遍历:先 左子树.右子树 ,最后再遍历根结点. 中序遍历:先左子树,再根 ...

  3. 1020. Tree Traversals (25)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1020 题目: 1020. Tree Traversals (25) 时间限制 400 ms 内存 ...

  4. PAT甲级1086 Tree Traversals Again:[C++题解]二叉树中序序列、栈、求后序遍历

    文章目录 题目分析 题目链接 题目分析 分析: 给定栈模拟的二叉树的中序序列. 我们可以发现一些性质: 1 第一个值是根结点. 2 对于所有的push操作,如果上一个是push,该结点就是上一个结点的 ...

  5. 伸展树(Splay tree)图解与实现

    伸展树(Splay tree)图解与实现 伸展树(Splay tree)图解与实现_小张的专栏-CSDN博客_splay树 Splay树详解 Splay树详解 - 秦淮岸灯火阑珊 - 博客园 平衡树 ...

  6. hdu 1710 Binary Tree Traversals (二叉树)

    1 /********************************************************** 2 题目: Binary Tree Traversals(hdu 1710) ...

  7. 1086 Tree Traversals Again

    1. 这题的核心部分是,根据二叉树的先序序列和中序序列求后序序列.等于是在1020 Tree Traversals这一题的基础上,把怎么得到先序序列和中序序列的难度加大了,不是直接给出,而是要曲折一点 ...

  8. 使用行为树(Behavior Tree)实现网游奖励掉落系统

    原地址:http://blog.csdn.net/akara/article/details/6165421 [原创]使用行为树(Behavior Tree)实现网游奖励掉落系统 by AKara 2 ...

  9. 数据结构与算法(C++)– 树(Tree)

    数据结构与算法(C++)– 树(Tree) 1.树的基础知识 树(tree): 一些节点的集合,可以为空集 子树(sub tree): 树的子集 根(root): 树的第一个节点 孩子和父亲(Chil ...

最新文章

  1. Halcon算子翻译——dev_set_line_width
  2. 20100506 学习记录:grdview添加新的一行数据
  3. App_GlobalResources、App_LocalResources (理论篇)(
  4. Firefox下代码触发a标签的click事件无效
  5. MyBatis 源码解读-mapperElement()
  6. ADN新开了云计算Cloud和移动计算Mobile相关技术的博客
  7. 什么是以太网光纤收发器,其产品特点和技术参数都有哪些?
  8. 怎么退出自适应巡航_简单聊聊定速巡航和自适应定速巡航的区别
  9. 智能机维修暴利大起底:触摸屏成本30维修300元
  10. UVA 12676 Inverting Huffman
  11. Combining Label Propagation and Simple Models Out-performs Graph Neural Networks 论文理解
  12. cdd matlab 算法,CDD图像修复
  13. 小程序 - canvas绘制海报
  14. 插入图片与背景图片的区别
  15. 深度学习数据集(一)
  16. cuckoo sandbox如何使用
  17. 中国电信增值业务是什么?一类和二类的区别是什么?
  18. Face Paper: 目标检测RSSD论文详解
  19. 金融学与计算机金融有什么区别,高考志愿填报之热门专业:金融vs计算机如何选择...
  20. iphone引用自定义字体 html,在iphone中使用自定义字体

热门文章

  1. 扩展DropDownList控件和ListBox控件(1) - 支持分组功能(optgroup标签)
  2. python产生随机数并排序_中小学python教学案例:随机数按升序排列 输出
  3. 文本聚类分析算法_常用的聚类分析算法综述
  4. gitglone 指定分支_安装说明 · harryxu/gitube Wiki · GitHub
  5. 学web前端好找工作吗?想给初学者们几点建议
  6. mysql产生大量数据_mysql语句批量产生大量测试数据
  7. linux复制文件到另一个目录_Linux入门之四-Linux文件目录操作
  8. CMake下载及安装
  9. 动物识别系统 c语言代码_C ++程序员避不开虚函数的,就像C语言程序员避不开指针一样...
  10. string能存多大数据_信息技术助力精准教学:大数据到底有多好用?