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. 随手练——HDU 1078 FatMouse and Cheese(记忆化搜索)

    http://acm.hdu.edu.cn/showproblem.php?pid=1078 题意: 一张n*n的格子表格,每个格子里有个数,每次能够水平或竖直走k个格子,允许上下左右走,每次走的格子 ...

  2. Chrome插件(扩展)开发全攻略

    [干货]Chrome插件(扩展)开发全攻略:https://www.cnblogs.com/liuxianan/p/chrome-plugin-develop.html

  3. 6、JUC--同步锁Lock

    显示锁 Lock  在Java 5.0之前,协调共享对象的访问时可以使用的机 制只有 synchronized 和 volatile .Java 5.0 后增加了一些 新的机制,但并不是一种替代内置 ...

  4. leetcode70—Climbing Stairs

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  5. python下载安装搭建

    python官网下载python运行环境(https://www.python.org/downloads/),建议下载稳定版本,不推荐使用最新版本 安装 然后我们打开CMD,在里面输入python, ...

  6. 解决:linux 固定ip 导致ping 外网unknown host

    首先说下问题产生场景:最近搞jenkins搭建持续集成,搞完后发现服务器ip(ifconfig 红色)老是变化,一怒之下果断修改ip [root@bogon etc]# ifconfigeth0 Li ...

  7. flask 入门(二)

    Windows(提前安好virtualenv:pip  install  virtualenv) 一.准备: 1.启动pycharm 2.创建flask项目 二.基本库安装和设置 1.创建沙盒virt ...

  8. Python爬虫爬取贴吧的帖子内容

    最近在看一个大神的博客,从他那里学会了很多关于python爬虫的知识,其实python如果想用在实际应用中,你需要了解许多,比如正则表达式.引入库.过滤字段等等,下面不多说,我下面的程序是爬取Ubun ...

  9. 大数据入门第十五天——HBase整合:云笔记项目

    一.功能简述 1.笔记本管理(增删改) 2.笔记管理 3.共享笔记查询功能 4.回收站 效果预览: 二.库表设计 1.设计理念 将云笔记信息分别存储在redis和hbase中. redis(缓存):存 ...

  10. 20155206 Exp2 后门原理与实践

    20155206 Exp2 后门原理与实践 1.Windows获得Linux Shell 在windows下,打开CMD,使用ipconfig指令查看本机IP 然后使用ncat.exe程序,ncat. ...