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 ...
随机推荐
- 由于没有公钥,无法验证下列签名: NO_PUBKEY 54422A4B98AB5139
gpg --keyserver pgpkeys.mit.edu --recv-key 54422A4B98AB5139 gpg -a --export 54422A4B98AB5139 | sudo ...
- AI1.1-人工智能史
来自:http://zh.wikipedia.org/wiki/人工智能史#CITEREFBerlinski2000 这篇是来自维基百科上面的人工智能史,将其大部分保留(真的是大部分,所以差不多没有原 ...
- sphinx搜索 笔记
架构图: 安装sphinx,见文章http://my.oschina.net/ptk/blog/495435 sphinx关键的配置文件.在里面写查询的sql. 两个关键命令:indexer生成查询索 ...
- 20155333 《网络对抗》Exp2 后门原理与实践
20155333 <网络对抗>Exp2 后门原理与实践 1.例举你能想到的一个后门进入到你系统中的可能方式? 下载的软件中捆绑有后门: 浏览的网页或其上的小广告: 有些网页会自动安装软件. ...
- Compensating-Transaction模式
在应用中,会将一系列相关的操作定义为一个连续的操作,当其中一个或者多个步骤失败的时候,Compensating-Transaction模式会重置(回滚)这个连续的操作.在云应用中,这些需要保证一致性的 ...
- Luogu P1558 色板游戏
(此题与POJ2777重题) 为了加深对线段树的记忆,然后开始搞这道题. TM的WA了一下午就是发现x可能大于y(然而题目里说的还很清楚,我TM没看见) 这道题只需要在线段树的板子上改一些地方就可以了 ...
- mfc 基类与子类
基类(父类) 派生类(子类) 一.基类(父类) 基类(又称为父类,基类与派生类是相对的关系! 通过继承机制,可以利用已有的数据类型来定义新的数据类型.所定义的新的数据类型不仅拥有新定义的成员,而且还同 ...
- 记录一次 @Autowired 无法注入( spring依赖正常 idea显示有spring已注入的图标)导致空指针异常的原因
首先,参考 https://blog.csdn.net/weixin_40475523/article/details/81085990 然后发现 是因为我把自己的这个类加上了 @Service 注解 ...
- [HNOI2018]转盘[结论+线段树]
题意 题目链接 分析 首先要发现一个结论:最优决策一定存在一种 先在出发点停留之后走一圈 的情况,可以考虑如下证明: 如果要停留的话一定在出发点停留,这样后面的位置更容易取到. 走超过两圈的情况都可以 ...
- 本地mysql快速迁移到服务器数据库中
我们可以使用linux的scp命令(scp无法在windows使用),加上mysql自带的mysqldump,能很快的完成数据库的迁移 将本地的数据库(music_db)导出为sql文件(music_ ...