7-5 Tree Traversals Again (25 分)
 

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 (≤) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2 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 <vector>
#include <stack>
#include <cstring> using namespace std; vector<int> pre,in,post,val; void postorder(int root,int start,int end){
if(start>end) return ;
int root_index=;
while(root_index<=end&&in[root_index]!=pre[root]) root_index++;
postorder(root+,start,root_index-);
postorder(root++root_index-start,root_index+,end);
post.push_back(pre[root]);
}
int main()
{
stack<int> sta;
int k=,N;
char ch[];int num;
scanf("%d",&N);
while(~scanf("%s",&ch)){
if(strlen(ch)==) {
scanf("%d",&num);
pre.push_back(num);
sta.push(num);
}else{
in.push_back(sta.top());
sta.pop();
if(in.size()==N) break;
}
}
postorder(,,N-);
for(int i=;i<N;i++)
if(i!=N-) printf("%d ",post[i]);
else printf("%d",post[i]); system("pause");
return ;
}

数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)的更多相关文章

  1. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

  2. PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习

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

  3. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  4. 1020 Tree Traversals (25 分)(二叉树的遍历)

    给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...

  5. A1020 Tree Traversals (25 分)

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

  6. 03-树3 Tree Traversals Again (25 分)

    An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...

  7. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

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

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

  9. 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历

    03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorde ...

随机推荐

  1. Ubuntu 16.04 RabbitMq 安装与运行

    前言目前公司用阿里云 + redis 的方式实现的消息队列.了解了目前几种主流的消息组件(主要包括rabbitmq.kafka.)的优缺点后,这里为了深入学习rabbitmq,我在自己的腾讯云服务器上 ...

  2. Jmeter安装及配置(傻瓜模式)

    接下来将以傻瓜模式进行安装,跟着流程走,没错的~ 1.首先进入到apache官网https://www.apache.org/dist/jmeter/binaries下载Windows版本JMeter ...

  3. selenium3关于ddt数据驱动。。

    from selenium import webdriver import ddt import time import unittest @ddt.ddt class TestLogin(unitt ...

  4. poj1222(高斯消元法解异或方程组+开关问题)

    题目链接:https://vjudge.net/problem/POJ-1222 题意:给定一个5×6的01矩阵,改变一个点的状态时它上下左右包括它自己的状态都会翻转,因为翻转2次等价与没有翻转,那么 ...

  5. Spark学习一:Spark概述

    1.1 什么是Spark ​ Apache Spark 是专为大规模数据处理而设计的快速通用的计算引擎. ​ 一站式管理大数据的所有场景(批处理,流处理,sql) ​ spark不涉及到数据的存储,只 ...

  6. Java学习路径

    -------第一部分:基础语法-------- 1.输出语句 1.1 hello world 1.2 拼接输出.换行和不换行输出 1.3 拼接变量输出 2.输入语句: 2.1 定义变量,赋值(整数. ...

  7. (二十八)动态盐的MD5加密算法(java实现)

    目录 文章目录 @[toc] 源代码: 函数用法讲解: 用法代码实例: 对比普通 **`MD5`** 的优点 实现思路: 后来我发现,BCryptPasswordEncoder 是这个思路的实现的最优 ...

  8. 用函数来编写实现strlen()函数功能

    strlen( )函数: 测试字符串实际长度的函数,它的返回值是字符串中字符的个数(不包含’\0’) //strlen( )函数:测试字符串实际长度的函数,它的返回值是字符串中字符的个数(不包含’\0 ...

  9. kubernetes kubeadm安装v1.14

    1.我们这里准备两台Centos7的主机用于安装,后续节点可以根究需要添加即可:master node01两台都得改:cat /etc/hosts192.168.71.134 master192.16 ...

  10. C#添加带验证的websevice接口

    记录一下,方便下次使用,或者能帮助到别人. 一.添加服务引用,输入WSDL文件地址. 二.代码 public TESTClient TestContext() { var binding = new ...