1086. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=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

提交代码

 #include<cstdio>
#include<stack>
#include<algorithm>
#include<iostream>
#include<stack>
#include<set>
#include<map>
#include<vector>
using namespace std;
int per[],in[];
stack<int> s;
int n;
void Build(int *per,int *in,int num){
if(num==){
return;
}
int mid=per[]; //cout<<mid<<endl; int i;
for(i=;i<num;i++){
if(in[i]==mid){
break;
}
} //cout<<num<<endl; Build(per+,in,i);
Build(per++i,in++i,num--i);
if(num==n){
printf("%d",mid);
}
else{
printf("%d ",mid);
}
}
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
scanf("%d",&n);
n*=;
int i,num,pi=,ini=;
string op;
for(i=;i<n;i++){
cin>>op;
if(op=="Push"){
scanf("%d",&num);
s.push(num);
per[pi++]=num;
}
else{
in[ini++]=s.top();
s.pop();
}
}
n=n/; /*cout<<n<<endl;
for(i=0;i<n;i++){
cout<<per[i]<<" ";
}
cout<<endl;
for(i=0;i<n;i++){
cout<<in[i]<<" ";
}
cout<<endl;*/ Build(per,in,n);
printf("\n");
return ;
}

pat1086. Tree Traversals Again (25)的更多相关文章

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

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

  2. PAT1086:Tree Traversals Again

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

  3. 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 ...

  4. pat03-树3. Tree Traversals Again (25)

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

  5. 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 ...

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

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

  7. 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)

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

  8. 1086. Tree Traversals Again (25)

    题目如下: An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For e ...

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

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

随机推荐

  1. C#.Net使用正则表达式抓取百度百家文章列表

    工作之余,学习了一下正则表达式,鉴于实践是检验真理的唯一标准,于是便写了一个利用正则表达式抓取百度百家文章的例子,具体过程请看下面源码: 一:获取百度百家网页内容 public List<str ...

  2. C# 链表 --增 -删-反转-删除最小值

    1. Node.cs namespace 链表 { public class Node<T> { public T Data; //这个就是地址 public Node<T> ...

  3. 小 M 的算式(dfs)

    [问题描述]小 M 在做数学作业的时候遇到了一个有趣的问题:有一个长度为 n 的数字串 S,小 M 需要在数字之间填入若干个“+”和恰好一个“=”,使其成为一个合法的等式.如对于 S=“2349”,可 ...

  4. PCANet: A Simple Deep Learning Baseline for Image Classification?--名词解释

    1 上采样与下采样 缩小图像(或称为下采样(subsampled)或降采样(downsampled))的主要目的有两个: 使得图像符合显示区域的大小 生成对应图像的缩略图 下采样原理:对于一幅图像I尺 ...

  5. SSH—Struts2拦截器的应用(防止未登录用户进行操作)

    前言 类似于京东.淘宝这些平台,如果单纯的去浏览页面上的一些商品显示,一点问题都没有,但是当你点击商品的订单详情或者想查看一下自己的购物车,那么就会出现通过登录进去的界面,这个就是今天要说的这个拦截器 ...

  6. springcloud系列11 整合微服务网关zuul

    这个模块是一个独立的模块所以需要建立一个模块, 首先引入: 依赖pom.xml <?xml version="1.0" encoding="UTF-8"? ...

  7. python 对三维CT数据缩放

    项目需要对CT数据进行缩放,这里我存储CT数据的格式是numpy数组. 一共尝试了三种方法,分别是numpy.resize,cv2.resize,scipy.ndimage.interpolation ...

  8. C语言中函数声明、形参、实参

    函数原型: 原型prototype是函数的声明:描述了函数的返回值与参数: 函数原型说明了两点: 1.该函数的返回值 2.该函数的参数及其类型 ++++++++++++++++++++++++++++ ...

  9. Linux 磁盘 分区 挂载点的理解

    在Linux中一切皆文件: 虚拟文件系统(Virtual File System, 简称 VFS), 是 Linux 内核中的一个软件层,用于给用户空间的程序提供文件系统接口:同时,它也提供了内核中的 ...

  10. http文件上传/下载

    package unit; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileOutputSt ...