题目

链接

分析

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. 20165213&20165225结对学习感想及创意照

    20165213&20165225结对学习感想及创意照 会JAVA的大学生活好小组 团队感悟: 1+1>2还是1+1<2? 上述两个观点实际没有对错之分,取决点在于个人见解. 相信 ...

  2. 使用脚本与orm模型交互对数据库操作

    场景:如不想启动服务在框架中查看数据库数据,同时使用ORM框架对数据库操作带来的好处 import os import sys #将脚本所在的工程添加到环境变量 sys.path.append('.. ...

  3. 洛谷P4570 [BJWC2011]元素 线性基

    正解:线性基+贪心 解题报告: 传送门! 这题其实没什么好写题解的,,,显然贪心一下尽量选魔力大的,不用证明趴挺显然的来着 所以就直接按魔力排个序,插入线性基里面,能插就加个贡献,over 放下代码趴 ...

  4. 【python基础】sys

    sys模块 参考: https://blog.csdn.net/qq_38526635/article/details/81739321 http://www.cnblogs.com/cherishr ...

  5. 看大师解说Android高速开发框架EasyAndroid

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/u010966622/article/details/37601789 前几天做了小应用.感觉小有成就 ...

  6. mysql explain中的 “Select tables optimized away”

    mysql explain中的 “Select tables optimized away” http://blog.chinaunix.net/uid-10449864-id-2956845.htm ...

  7. 接口,定义接口的关键字是 interface 实现接口关键字是 implements

    当抽象类中的方法都是抽象的时候,这时可以将该抽象类用另一种形式定义和表示,就是接口interface特点:对于接口中常见的成员:而且这些成员都有固定的修饰符,不写就默认存在1:全局常量 :  都是 p ...

  8. 使用Python自带difflib模块进行文件内容差异对比

    difflib_text.py #!/usr/bin/python import difflib import sys try: textfile1=sys.argv[1] textfile2=sys ...

  9. [ Linux运维学习 ] 路径及实战项目合集

    我们知道运维工程师(Operations)最基本的职责就是负责服务的稳定性并确保整个服务的高可用性,同时不断优化系统架构.提升部署效率.优化资源利用率,确保服务可以7*24H不间断地为用户提供服务. ...

  10. AC自动机模板2

    题目链接:https://www.luogu.org/problemnew/show/P3796 #include <cstdio> #include <cmath> #inc ...