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的更多相关文章

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

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

  2. PAT 甲级 1020 Tree Traversals (二叉树遍历)

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

  3. PAT 甲级 1020 Tree Traversals (25分)(后序中序链表建树,求层序)***重点复习

    1020 Tree Traversals (25分)   Suppose that all the keys in a binary tree are distinct positive intege ...

  4. PAT 甲级 1020 Tree Traversals (25 分)(二叉树已知后序和中序建树求层序)

    1020 Tree Traversals (25 分)   Suppose that all the keys in a binary tree are distinct positive integ ...

  5. PAT 甲级 1020 Tree Traversals

    https://pintia.cn/problem-sets/994805342720868352/problems/994805485033603072 Suppose that all the k ...

  6. PAT甲级——A1086 Tree Traversals Again

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

  7. PAT甲级——A1020 Tree Traversals

    Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...

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

  9. 1086 Tree Traversals Again——PAT甲级真题

    1086 Tree Traversals Again An inorder binary tree traversal can be implemented in a non-recursive wa ...

随机推荐

  1. HDU1599(Floyd最小环)

    Floyd最小环理解+模板: https://www.cnblogs.com/DF-yimeng/p/8858184.html 除了上述博文里写的,我再补充几点我的理解. 1.为什么先枚举ij求经过i ...

  2. Android 给TextView中的字体加上“中间线”

    大家都知道在做购物App或者购物网站的时候,商品价格往往会有一个“现价”和“原价”而原价往往会在中间加上一个黑色的横线.便于醒目客户,但是这种效果在App中应该怎样做呢? 废话不多少,直接给大家看代码 ...

  3. nodeSelector + deamonset

    DaemonSet 配置文件的语法和结构与 Deployment 几乎完全一样,只是将 kind 设为 DaemonSet. 选择运行节点:当指定.spec.template.spec.nodeSel ...

  4. (三)Lua脚本语言入门(数组)

    又要找工作了,变的忧虑了,唯有学习才让内心变得踏实,今天玩了一下午的王者荣耀,正事都忘了...... 如果认为所谓的毅力是每分每秒的“艰苦忍耐”式的奋斗,那这是一种很不足的心理状态.毅力是一种习惯,毅 ...

  5. 学会如何使用Github进行托管代码和用markdown撰写心得

    这次作业是学会如何使用Github进行托管代码和用markdown撰写心得. 1.掌握使用Git进行代码版本,使用github进行代码托管.(git使用,推荐imooc公开课: 公开课 ) 登录 gi ...

  6. gcc 动态编译 动态库路径

    gcc 动态编译(共享库) 动态编译的可执行文件需要附带一个的动态链接库,在执行时,需要调用其对应动态链接库中的命令优点:体积小,编译快缺点:依赖性高 代码如下: [root@74-82-173-21 ...

  7. Ubuntu14.04配置gcc4.4.4+Qt4.8.4交叉编译环境

    安装32位程序运行支持 sudo apt-get install lib32stdc++6 lib32z1 lib32ncurses5 lib32bz2-1.0 可能报错: lib32stdc++6 ...

  8. Android开发——进程间通信之Bundle和文件

    0.  前言 不论是Android还是其他操作系统,都会有自己的IPC机制,所谓IPC(Inter-Process Communication)即进程间通信.首先线程和进程是很不同的概念,线程是CPU ...

  9. oracle10g安装在裸设备上

    参考了百度文库上的 <在​裸​设​备​上​面​安​装​o​r​a​c​l​e​1​0​g> 一文. 不过为了简单起见,我选择OS 为 Redhat AS 4.8 32位. 准备安装数据库为 ...

  10. 汇编 EAX,EBX,ECX,EDX,寄存器

    知识点: 寄存器EAX 寄存器AX 寄存器AH 寄存器AL 一.EAX与AX,AH,AL关系图 一格表示一字节 #include <Windows.h> int _tmain(int ar ...