03-树3. Tree Traversals Again (25)

时间限制
200 ms
内存限制
65536 kB
代码长度限制
8000 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<algorithm>
#include<iostream>
#include<cstring>
#include<queue>
#include<stack>
using namespace std;
queue<int> q;
void Buildin(int *pre,int *in,int inlen){
if(!inlen){
return;
}
int c=pre[];
int i;
for(i=;i<inlen;i++){
if(c==in[i]){
break;
}
}
Buildin(pre+,in,i);
Buildin(pre+i+,in+i+,inlen-i-);
q.push(pre[]);
}
int pre[],in[];
int main(){
//freopen("D:\\INPUT.txt","r",stdin);
//freopen("D:\\OUTPUT.txt","w",stdout);
int n;
string op;
stack<int> s;
//int *pre=new int[n+1];
//int *in=new int[n+1];
scanf("%d",&n); //cout<<n<<endl; int i,prep=,inp=;
n*=;
for(i=;i<n;i++){
cin>>op;
if(op=="Push"){
scanf("%d",&pre[prep]);
s.push(pre[prep++]);
}
else{
in[inp++]=s.top();
s.pop();
}
} /*for(i=0;i<n/2;i++){
cout<<pre[i]<<endl;
}
cout<<endl;
for(i=0;i<n/2;i++){
cout<<in[i]<<endl;
}
cout<<endl;
cout<<i<<endl;*/ Buildin(pre,in,n/); /* cout<<endl;
cout<<i<<endl;*/ printf("%d",q.front());
q.pop();
while(!q.empty()){
printf(" %d",q.front());
q.pop();
}
return ;
}

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

  3. pat1086. Tree Traversals Again (25)

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

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

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

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

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

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

  7. PAT Advanced 1086 Tree Traversals Again (25) [树的遍历]

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

  8. 1086. Tree Traversals Again (25)-树的遍历

    题意:用栈的push.pop操作给出一棵二叉树的中序遍历顺序,求这棵二叉树的后序遍历. 需要一个堆结构s,一个child变量(表示该节点是其父亲节点的左孩子还是右孩子),父亲节点fa对于push v操 ...

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

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

随机推荐

  1. LOJ#10172. 「一本通 5.4 练习 1」涂抹果酱

    题目链接:https://loj.ac/problem/10172 题目描述 Tyvj 两周年庆典要到了,Sam 想为 Tyvj 做一个大蛋糕.蛋糕俯视图是一个 N×MN×MN×M 的矩形,它被划分成 ...

  2. php 递归删除目录

    /* * 递归删除目录 */ function deletedir($dir){ if($handle = @opendir($dir)){ while($file = readdir($handle ...

  3. C# LINQ(2)

    前一章的代码LINQ都是以select结尾. 之前也说过可以group结尾. 那么怎么使用呢? 还是一样的条件,查询小于5大于0的元素 代码: ,,,,,,,,, }; var list = from ...

  4. ubuntu安装nginx与配置

    命令行安装:(当前时间为2018.11,版本为1.10.3) sudo apt-get install nginx 安装好的文件位置: /usr/sbin/nginx:主程序 /etc/nginx:存 ...

  5. Eclipse内存不足 增加eclipse的运行内存

    自己解决的 三.    修改Run Configurations (此方法可行) 在代码上右键,依次点击“Run As ”-> “Run Configurations ”,在Arguments  ...

  6. java中容器的概念

    容器:顾名思义,装东西的器物至于spring中bean,aop,ioc等一些都只是实现的方式具体容器哪些值得我们借鉴,我个人觉得是封装的思想.将你一个独立的系统功能放到一个容器之中,可以当做一个大的接 ...

  7. 【spring源码】spring web 启动与关闭

    web.xml中有这么一段声明 <context-param> <param-name>contextConfigLocation</param-name> < ...

  8. Reviewing notes 1.1 of Analytic geometry

    Chapter 1 Vector Algebra ♦ Vector Space Vector and vector space A vector is described as a quantity ...

  9. 数据结构54:平衡二叉树(AVL树)

    上一节介绍如何使用二叉排序树实现动态查找表,本节介绍另外一种实现方式——平衡二叉树. 平衡二叉树,又称为 AVL 树.实际上就是遵循以下两个特点的二叉树: 每棵子树中的左子树和右子树的深度差不能超过 ...

  10. ansible基本模块-yum

    ansible   XXX   -m   yum   -a  "name=XXX"