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. windows phone之依赖属性(DependencyProperty)

    Windows Presentation Foundation (WPF) 提供了一组服务,这些服务可用于扩展公共语言运行时 (CLR) 属性的功能,这些服务通常统称为 WPF 属性系统.由 WPF ...

  2. webrequest、httpwebrequest、webclient、HttpClient 四个类的区别

    一.在 framework 开发环境下: webrequest.httpwebreques  都是基于Windows Api 进行包装, webclient 是基于webrequest 进行包装:(经 ...

  3. SQL SERVER2008 R2修改Server Collation--文檔沒細看先存下來.

    HOW TO: RESTORE THE MASTER DATABASE IN SQL SERVER 2012 by Thomas LaRock | Jan 14, 2014 | MSSQL, SQL ...

  4. [转]Replace all UUIDs in an ATL COM DLL.

    1. Introduction. 1.1 Recently, a friend asked me for advise on a very unusual requirement. 1.2 He ne ...

  5. Mysql高性能优化规范建议

    数据库命令规范 所有数据库对象名称必须使用小写字母并用下划线分割 所有数据库对象名称禁止使用mysql保留关键字(如果表名中包含关键字查询时,需要将其用单引号括起来) 数据库对象的命名要能做到见名识意 ...

  6. python-flask之request的属性

    flask的request模块的属性(参考) #代码示例,仅仅是为了测试request的属性值 @app.route('/login', methods = ['GET','POST']) def l ...

  7. php代码审计3审计sql注入漏洞

    SQL注入攻击(sql injection)被广泛用于非法获取网站控制权,在设计程序时,忽略或过度任性用户的输入,从而使数据库受到攻击,可能导致数据被窃取,更改,删除以及导致服务器被嵌入后门程序等 s ...

  8. 获取表单提交的数据getParameter()方法

    请求对象:request public String getParameter(String name); 通过request的getParameter(String name)方法获取 表单里面的n ...

  9. python中html解析

    import requestsfrom bs4 import BeautifulSoup url = "..." payload =...headers = None respon ...

  10. centos安装mysql57

    下载源安装文件 https://dev.mysql.com/downloads/repo/yum/ wget http://repo.mysql.com//mysql57-community-rele ...