题目

链接

分析

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. PHP之错误

    三.PHP配置之Error handling logging 1.error_reporting integer error_reporting = E_ALL 设置错误报告的级别.该参数可以是一个任 ...

  2. Java 输入/输出——Java虚拟机读写其它进程的数据

    使用 Runtime对象的exec()方法可以运行平台上的其它程序,该方法产生一个Process对象,Process对象代表由该Java程序启动的子进程.Process类提供了如下三个方法,用于让程序 ...

  3. base64简单使用

    加密: import base64 import random str ="aqwertyuiopasdfghjklzxcvbnm963.0852741,.;'" a = '人生苦 ...

  4. 对于python setup install的程序如何删除干净

    python很好用,尤其是用过easy_install的朋友更是觉得它的便捷,卸载命令也很简单 easy_install -m package-name但是面对源码安装的怎么办呢? setup.py ...

  5. nodejs 学习二, nodejs调试

    nodejs 调试,在官方文档(英文),常用的两种: 一个在chrome安装插件inspector 第二种利用编辑 这里我使用vscode编辑来调试. 主要是配置 launch.json(用vscod ...

  6. $(this) 和 this 关键字在 jquery 中有何不同?

    $(this) 返回一个 jQuery 对象,你可以对它调用多个 jQuery 方法,比如用 text() 获取文本,用 val() 获取值等等. 而 this 代表当前元素,它是 javascrip ...

  7. 剑指offer-矩阵覆盖

    题目描述 我们可以用2*1的小矩形横着或者竖着去覆盖更大的矩形.请问用n个2*1的小矩形无重叠地覆盖一个2*n的大矩形,总共有多少种方法?   使用dp,当n时,选着竖着放一个,那么后面的可能性为f( ...

  8. Vue通过build打包后 打开index.html页面是空白的

    最近在build打包vue项目遇到了几个问题,如下: 1.npm run build打包项目之后,我们通常是把dist文件里面被压缩后的static文件跟index.html提交到服务器,但最近发现直 ...

  9. SQL Server 安装好后 Always On群组配置

    需要对SQL Server必要的端口打开Windows防火墙的入站规则,比如TCP- 1433端口等. 特别注意:由于AG默认需要用到TCP-5022端口,所以该端口务必保证在Windows防火墙中开 ...

  10. 技嘉主板BIOS恢复方法

    技嘉的dual bios技术的原理很简单,在main bios能启动的情况下,backup bios不会对bios进行检测.只有当main bios出现故障(这个故障一般是软件故障)才会从backup ...