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 ...
随机推荐
- 静态性能测试-hc课堂笔记
UI自动化,需要掌握html相关知识 w3c网站. 会了性能测试就会了接口自动化. 静态扫描:降低40-50% findbugs,隐含的bug checkstyle,风格规范 域名解析: 输入网址-D ...
- python学习课件
张鑫 18511446896 ------------------------------------------------------------------------------------- ...
- k-center问题-学习
k-center问题: In graph theory, the metric k-center or metric facility location problem is a combinator ...
- 关于PLC高速计数器使用
今天去面试问我高速计数器,因为没用过,所以直接说--不会.但是自己感觉自己自学电气,说不会太丢人了,所以今天学了PLC的高速计数器.虽然没有书,但是有度娘,还有现成的PLC设备实际检验程序,更有鹏哥和 ...
- MVC 在action方法中获取当前action的控制器名和action名
如何在某个action方法中获取它所在的控制器和action名称呢. string controllerName = Request.RequestContext.RouteData.Values[& ...
- Swift10大开源项目记录
Alamofire : Swift编写的HTTP网络库,用于异步网络通信. Surge: Surge基于Accelerate框架开发,用于执行矩阵数学.数字信号处理以及图像处理等方面. SwiftyJ ...
- 20155234 昝昕明《基于ARM实验箱的国密算法应用》课程设计个人报告
20155234 昝昕明<基于ARM实验箱的国密算法应用>课程设计个人报告 个人贡献 参与课设题目讨论及完成全过程: 资料收集: SM1算法及和ARM之间通信 负责串口代码调试: 协调完成 ...
- 20155338 《网络攻防》 Exp7 网络欺诈防范
20155338 <网络攻防> Exp7 网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击 在一些公共场所,看到有免费的公用WIFI就想连的时候就容易受到 在日常 ...
- Luogo P2324 [SCOI2005]骑士精神
所有想练习A*的人都先来敲一下这道题吧. 数据范围即便只有5*5,但朴素的爆搜还是会超时. 因此考虑剪枝. 对于这道题,肯定只要进行最优化剪枝,判断现在走的步数+剩下最少要走的步数,如果大于ans或者 ...
- VS与Opencv的亲密接触之安装配置过程
最近想把FPGA采集的图像,上传到上位机显示,看到Opencv能帮大忙,所以就折腾折腾! 我用的是VS2012和opencv-2.4.10-2.4.10(目前的最新版本),那个版本无所谓,本文都将适用 ...