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. 国内物联网平台(3):QQ物联智能硬件开放平台

    国内物联网平台(3)——QQ物联·智能硬件开放平台 马智 平台定位 将QQ帐号体系.好友关系链.QQ消息通道及音视频服务等核心能力提供给可穿戴设备.智能家居.智能车载.传统硬件等领域的合作伙伴,实现用 ...

  2. android studio中使用recyclerview小白篇(一)

    本人就是小白,昨天在使用listview时,看到说有更好的控件出来了,在V7包中,需要SDK21及以上,那就试着用用吧,今天试了一天,终于弄的能简单使用了,分享一下. 怎么导入这个recycleyvi ...

  3. WSMBT Modbus & WSMBS Modbus 控件及注册机

    先上注册机 点击下载 How to add the WSMBT control to the toolbox: On the Tools menu, click Choose Toolbox Item ...

  4. 关于Logger

    Logger是我在各类编程语言中使用最多,同时也是改进最多的一个函数,今天在iOS下又折腾了一番,终于找到我想要的一个版本,这里做一个总结. python版 python对logger有专门的支持,只 ...

  5. UIPasteboard

    1.UIPasteboard 简介 顾名思义,UIPasteboard 是剪切板功能,因为 iOS 的原生控件 UITextField.UITextView.UIWebView, 我们在使用时如果长按 ...

  6. K-Dominant Character (模拟)

    You are given a string s consisting of lowercase Latin letters. Character c is called k-dominant iff ...

  7. winform datagridview不显示滚动条

    datagridview 数据行数已经超出表格显示范围了,为什么右侧没有滚动条呢? 这个其实不是DataGridView的问题,实际上滚动条是出现了的,但被其他东西挡住了.如果网格是放在panel上的 ...

  8. B君的第九题

    B君的第九题 对于一个排列\(a_1, a_2,\dots,a_n\),如果对于一个i满足\(a_{i-1}<a_i>a_i+1\)则称i是一个极大值.我们认为\(a_0=a_{n+1}= ...

  9. 2019.2.14 考试T1 FFT

    \(\color{#0066ff}{ 题目描述 }\) 衡水二中的机房里经常有人莫名其妙地犇雷,leizi很生气,决定要找出那个犇雷的人 机房有n个人,每个人都认为机房里有两个人可能会犇雷,其中第i个 ...

  10. 字符串格式化str.format

    一.字符串格式化之str.format 1.位置参数:用{0},{1},{2}表示位置 v1 = '{1},{0},{1}'.format('a','b') #输出b,a,b 2.关键词参数:用{na ...