03-树3 Tree Traversals Again(25 分)
题目
分析
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 分)的更多相关文章
- 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 ...
- PAT 甲级 1086 Tree Traversals Again (25分)(先序中序链表建树,求后序)***重点复习
1086 Tree Traversals Again (25分) An inorder binary tree traversal can be implemented in a non-recu ...
- 数据结构课后练习题(练习三)7-5 Tree Traversals Again (25 分)
7-5 Tree Traversals Again (25 分) An inorder binary tree traversal can be implemented in a non-recu ...
- 【PAT甲级】1086 Tree Traversals Again (25 分)(树知二求一)
题意:输入一个正整数N(<=30),接着输入2*N行表示栈的出入(入栈顺序表示了二叉搜索树的先序序列,出栈顺序表示了二叉搜索树的中序序列),输出后序序列. AAAAAccepted code: ...
- A1020 Tree Traversals (25 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- PAT A1020 Tree Traversals (25 分)——建树,层序遍历
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and i ...
- 1020 Tree Traversals (25 分)(二叉树的遍历)
给出一个棵二叉树的后序遍历和中序遍历,求二叉树的层序遍历 #include<bits/stdc++.h> using namespace std; ; int in[N]; int pos ...
- 03-树3 Tree Traversals Again (25 分)
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example ...
- 03-树2. Tree Traversals Again (25)
03-树2. Tree Traversals Again (25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue ...
- 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 ...
随机推荐
- Xcode编译警告Assigning to 'id<XXXDelegat> ——Nullable' from incompatible type 'XXXView *const_strong'
编译报警告 可能是 自定义分类使用协议时出现与父类协议的冲突 解决方法如下:
- Log4j使用注意点
Porting log4j到指定项目的时候需要注意: 1. log4j选择字符集的时候通过CMake来更改配置,防止出错; 2.
- ios APP改名推送名字还是旧的
重启手机就行了 https://community.jiguang.cn/t/ios-app/14759
- Python生成器表达式
https://www.cnblogs.com/liu-shuai/p/6098218.html 简介: 生成器表达式并不真正的创建数字列表,而是返回一个生成器对象,此对象在每次计算出一个条目后,把这 ...
- day2_抓包-抓包工具Charles
1.Charles功能简单描述 1)定位问题,前端的.后端的问题 2)发出去的请求,请求头.请求体,返回的数据 3)拦截请求,修改请求 2.Charles抓包(Android手机) 1.要求手机得和你 ...
- Linux忘记密码常用的几种解决方法
原文链接:https://www.cnblogs.com/vurtne-lu/p/6550590.html 一. lilo引导1. 在出现 lilo: 提示时键入 linux single Boot: ...
- Redis所支持的数据结构
1.启动Redis2.Redis所支持的数据结构 2.1.Redis常用操作 2.2.String类型及操作 2.3.Hash类型及操作 2.4.List类型及操作 2.5.Set类型及操作 2.6. ...
- matlab中的常用函数
1.将变量a保存到a.txt,每个值中间用tab隔开 dlmwrite('a.txt',a,'\t'); 2.对比两个矩阵是否完全一样 isequal(a,b); %返回logic=1,则完全相同 3 ...
- RzCheckTree基本使用
procedure TForm1.Button1Click(Sender: TObject); var i: Integer; begin //循环读取勾选节点代码及内容 //StateIndex 1 ...
- Kinect2.0点云数据获取
接上一篇:Kinect2.0获取数据 http://blog.csdn.net/jiaojialulu/article/details/53087988 博主好细心,代码基本上帖过来就可以用,注释掉的 ...