题目

链接

分析

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. CALayer的子类之CAShapeLayer

    一,CAShapeLayer介绍 * CAShapeLayer继承自CALayer,属于QuartzCore框架,可使用CALayer的所有属性.   CAShapeLayer是在坐标系内绘制贝塞尔曲 ...

  2. [knowledge][linux][sysfs] sysfs文件系统

    https://en.wikipedia.org/wiki/Sysfs http://man7.org/linux/man-pages/man5/sysfs.5.html https://www.ke ...

  3. Calcite - StreamingSQL

    https://calcite.apache.org/docs/stream.html   Calcite's SQL is an extension to standard SQL, not ano ...

  4. Struts2中.properties文件放置路径(classpath)

    一.web应用的classpath简介   classpath路径,即WEB-INF下面的classes目录,所有src目录下面的java.xml.properties等文件编译后都会在此. Stru ...

  5. 元素class的增、删、查、toggle

    比如有一个元素div <div class="btn user">我是div</div> 之前只知道元素有一个className可以来改动  元素的类名 但 ...

  6. javascript 实例 静态 公共 私有

    传统 javascript 的原型对象 和 ts的类 对比 传统原型队形说明: //对象构造函数 function Atest(name) { //私有属性,只能在对象构造函数内部使用 var cla ...

  7. mysql from dual插入实现不插入重复记录

    在mysql中插入一或者多条记录的时候,要求某个字段的值唯一,但是该字段没有添加唯一性索引,可用from dual解决. select * from (select '2015080109' a,2 ...

  8. JavaScript学习(五)

  9. w命令 查看系统负载

    linux命令 w [root@localhost snmp]# w :: up :, user, load average: 0.00, 0.01, 0.05 USER TTY FROM LOGIN ...

  10. 帝国cms调用相关文章若没有则调取最新文章

    前面我们说了帝国cms调用最新文章 利用文字调用标签phomenews,现在我们说下相关文章的调用,如果文章有设置关键词的话可以直接用[!--other.link--]进行调取,现在我们来升级一下,如 ...