PAT 甲级 1086 Tree Traversals Again
https://pintia.cn/problem-sets/994805342720868352/problems/994805380754817024
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 <bits/stdc++.h>
using namespace std; int N;
vector<int> in, post, pre, val; void postorder(int root, int st, int en) {
if(st > en) return;
int i = st;
while(i < en && in[i] != pre[root]) i ++;
postorder(root + 1, st, i - 1);
postorder(root + 1 + i - st, i + 1, en);
post.push_back(pre[root]);
} int main() {
scanf("%d", &N);
stack<int> s;
string op;
int cnt = 0;
for(int t = 0; t < N * 2; t ++) {
cin >> op;
if(op == "Push") {
int x;
scanf("%d", &x);
pre.push_back(cnt);
val.push_back(x);
s.push(cnt ++);
} else {
in.push_back(s.top());
s.pop();
}
} postorder(0, 0, N - 1);
for(int i = 0; i < N; i ++) {
printf("%d", val[post[i]]);
printf("%s", i != N - 1 ? " " : "");
} return 0;
}
push 的顺序是前序遍历的顺序 按照题目 pop 得到的中序遍历的顺便 in 和 pre 存的是数字的位置 val 求数字的值 递归求出后序遍历
PAT 甲级 1086 Tree Traversals Again的更多相关文章
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- PAT 甲级 1020 Tree Traversals (二叉树遍历)
1020. Tree Traversals (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Suppo ...
- PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习
1020 Tree Traversals (25分) Suppose that all the keys in a binary tree are distinct positive intege ...
- PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)
1020 Tree Traversals (25 分) Suppose that all the keys in a binary tree are distinct positive integ ...
- PAT 甲级 1020 Tree Traversals
https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...
- PAT甲级——A1086 Tree Traversals Again
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- PAT甲级——A1020 Tree Traversals
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 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 ...
- 1086 Tree Traversals Again——PAT甲级真题
1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...
随机推荐
- json 压缩中文不转码
$testJSON=array('name'=>'中文字符串','value'=>'test'); echo json_encode($testJSON, JSON_UNESCAPED_U ...
- nmap数据流
扫描者:1.1.1.1被扫描者:2.2.2.2 0x00 介绍 在日常工作对目标信息收集时,我们经常用到nmap这款网络探测工具和安全/端口扫描器,虽然我们关注的是结果(如目标开启了哪些危险端口,什么 ...
- 三、并行编程 - Task同步机制。TreadLocal类、Lock、Interlocked、Synchronization、ConcurrentQueue以及Barrier等
在并行计算中,不可避免的会碰到多个任务共享变量,实例,集合.虽然task自带了两个方法:task.ContinueWith()和Task.Factory.ContinueWhenAll()来实现任务串 ...
- JAVA框架 Spring 入门
一.阐述: IoC:我们以前写的框架虽然我们已经进行分层,web.业务层.持久层.但是各个层之间的关系.耦合性比较高,那个层调用其他层的时候,需要new对应层的类的对象,这样的话,我们以后做修改的时候 ...
- Android Fragment(三)ListFragment简单介绍以及Fragment之间通信
一.Fragment通信简单介绍:Fragments之间是不能够直接通信的,他们之间的通信是通过Activity这个中间件来通信的, 为了让Fragment跟它的Activity通信,我们可以在Fra ...
- PRML2-概率分布
本博文来自<PRML第二章> 在第一章中说了对于模式识别问题来说,核心角色就是概率论.本章的目的一方面是为了介绍概率分布,另一方面也是为了对后面遇到的那些复杂问题先打下基础.本章关于分布上 ...
- 【转】MySQL执行计划分析
原文:http://www.cnblogs.com/wangyanhong/archive/2013/09/18/3327919.html 一.语法explain <sql语句>例如: e ...
- 【本地服务器】利用openssl生成证书
(一)下载openssl软件,解压,进入bin目录 下载地址 (二)1.在当前bin目录,按住shift键右击,选择"在此处打开命令窗口" 2.打开cmd命令窗口之后,在窗口中输入 ...
- Java基础系列篇:JAVA多线程 并发编程
一:为什么要用多线程: 我相信所有的东西都是以实际使用价值而去学习的,没有实际价值的学习,学了没用,没用就不会学的好. 多线程也是一样,以前学习java并没有觉得多线程有多了不起,不用多线程我一样可以 ...
- 分享一下个人学PS的过程
得知Photoshop这款软件是在上大学的时候,2010年.学校学生会的科技部纳新,要求新人会PPT.word.Excel和Photoshop.当时有一个Photoshop大神,成为了学生会科技部的主 ...