题目

链接

分析

push是二叉树前序遍历的结果,pop是二叉树中序遍历的结果,所以这个题就是已知前序遍历和中序遍历,求后序遍历。

AC代码

#include "bits/stdc++.h"
using namespace std;
struct TreeNode
{
int left=-1, right=-1;
}tree[45];
vector<int> v;
void postorder(int x) {
if (x == -1) return;
postorder(tree[x].left);
postorder(tree[x].right);
//cout << x << ' ';
v.push_back(x);
} int main() {
int n, i, x, t;
cin >> n;
string str;
stack<int> s;
t = 0;
bool p = true;
for (i = 1; i <= 2*n; i++) {
cin >> str;
if (str == "Push") {
cin >> x;
s.push(x);
if (p)
tree[t].left = x;
else
tree[t].right = x;
t = x;
p = true;
}
else if (str == "Pop") {
t = s.top();
p = false;
s.pop(); }
else {
cout << "出错了!";
return 0;
}
}
//后序遍历二叉树
postorder(0); //输出结果
for (i = 0; i < v.size() - 1; i++) {
cout << v[i];
if (i < v.size() - 2)
cout << ' '; } return 0;
}

03-树3 Tree Traversals Again(25 分)的更多相关文章

  1. PTA 03-树3 Tree Traversals Again (25分)

    题目地址 https://pta.patest.cn/pta/test/16/exam/4/question/667 5-5 Tree Traversals Again   (25分) An inor ...

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

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

  3. 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)

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

  4. 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)

    题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...

  5. A1020 Tree Traversals (25 分)

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

  6. PAT A1020 Tree Traversals (25 分)——建树,层序遍历

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

  7. 1020 Tree Traversals (25 分)(二叉树的遍历)

    给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...

  8. 03-树3 Tree Traversals Again (25 分)

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

  9. 03-树2. Tree Traversals Again (25)

    03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...

  10. 03-树3. Tree Traversals Again (25)将先序遍历和中序遍历转为后序遍历

    03-树3. Tree Traversals Again (25) 题目来源:http://www.patest.cn/contests/mooc-ds/03-%E6%A0%913 An inorde ...

随机推荐

  1. linux fix the superblock by dumpe2fs fsck

    It seems that you have a bad superblock. To fix this: Firstly, boot into a live CD or USB Find out y ...

  2. [development][C] C语言标准

    GUN C的标准文档: 也就是glibc https://www.gnu.org/software/libc/ http://man7.org/linux/man-pages/dir_section_ ...

  3. Flink – Stream Task执行过程

    Task.run if (invokable instanceof StatefulTask) { StatefulTask op = (StatefulTask) invokable; op.set ...

  4. Flink – SlotSharingGroup

      SlotSharingGroup 表示不同的task可以共享slot,但是这是soft的约束,即也可以不在一个slot 默认情况下,整个StreamGraph都会用一个默认的"defau ...

  5. 配置ASM以及创建恢复目录

    本次配置ASM沿用了搭建RAC的环境配置,系统选用CENTOS6.8 首先本地配置YUM,安装GRID集群件所需要的RPM包 [root@rac01 Packages]# cd /etc/yum.re ...

  6. MSSQL2008 部署及开启远程连接

    最近不少用户在windows2003 server 32位操作系统上安装SQL Server2008总是失败,出现大量错误.今天经过通过我反复测试安装,找出了一个便捷的安装方法,节省大家宝贵时间,具体 ...

  7. linux相关介绍

    1.linux的简介 (1)linux是一个开源.免费的操作系统,其稳定性.安全性.处理多并发(基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU) 的操作系统.linux是一个Unix类 ...

  8. ORACLE安装入门篇OEL5.4安装ORACLE11g

    一.安装ORACLE11g软件(11.2.0.0) (一)安装前的包支持 1.检测yum仓库是否已经配置好 yum list all 2.搭建yum仓库 1).挂载所需要的安装光盘 虚拟机挂载光盘: ...

  9. 异常的Error与Exception

    一般不可处理:Error:是jvm抛出的严重性问题.已经严重影响程序执行  例如:内存溢出等情况这种问题发生一般不针对处理,直接修改程序. 可处理:Exception: 该体系的特点:子类的后缀名都是 ...

  10. 【Java】-NO.16.EBook.4.Java.1.011-【疯狂Java讲义第3版 李刚】- AWT

    1.0.0 Summary Tittle:[Java]-NO.16.EBook.4.Java.1.011-[疯狂Java讲义第3版 李刚]-  AWT Style:EBook Series:Java ...