PAT甲级——【牛客练习A1004】
题目描述
An inorder binary tree traversal can be implemented in a non-recursive way with a stack. For example, suppose that when a 6-node binary tree (with the keys numbered from 1 to 6) is traversed, the stack operations are: push(1); push(2); push(3); pop(); pop(); push(4); pop(); pop(); push(5); push(6); pop(); pop(). Then a unique binary tree (shown in Figure 1) can be generated from this sequence of operations. Your task is to give the postorder traversal sequence of this tree.
Figure 1
输入描述:
Each input file contains one test case. For each case, the first line contains a positive integer N (<=30) which is the total number of nodes in a tree (and hence the nodes are numbered from 1 to N). Then 2N lines follow, each describes a stack operation in the format: "Push X" where X is the index of the node being pushed onto the stack; or "Pop" meaning to pop one node from the stack.
输出描述:
For each test case, print the postorder traversal sequence of the corresponding tree in one line. A solution is guaranteed to exist. All the numbers must be separated by exactly one space, and there must be no extra space at the end of the line.
输入例子:
6
Push 1
Push 2
Push 3
Pop
Pop
Push 4
Pop
Pop
Push 5
Push 6
Pop
Pop
输出例子:
3 4 2 6 5 1
解题思路:
根据题意可知, 栈的数据压入为二叉树的前序,栈的弹出为中序,求后序遍历
一般是通过前序、中序来构造二叉树,然后在遍历出后序遍历即可
版本一:
该版本的缺陷是,当出现相同数字时,无法在中序中确定谁是根节点!
#include <iostream>
#include <stack>
#include <vector> using namespace std; struct Node
{
int val;
Node* l;
Node* r;
Node(int a = -) :val(a), l(nullptr), r(nullptr) {} }; //通过前序、中序构造二叉树
Node* Create(const vector<int>dataPre, const vector<int>dataOrd,
int preL, int preR, int ordL, int ordR)//数据源,前序的左右边界,中序的左右边界
{
if (preL < preR)
return nullptr;
Node* root = new Node();
root->val = dataPre[preL];//根节点
int k = ordL;
while (dataOrd[k] != dataPre[preL])
++k;
k = k - ordL;//左子树个数
root->l = Create(dataPre, dataOrd, preL + , preL + k, ordL, ordL + k - );//构造左子树
root->r = Create(dataPre, dataOrd, preL + k + , preR, ordL + k + , ordR);//构造右子树
return root;
} //后序遍历
void LastTravle(vector<int>&res, Node* root)
{
if (root == nullptr)
return;
LastTravle(res, root->l);
LastTravle(res, root->r);
res.push_back(root->val);
} int main()
{
int N;
cin >> N;
N = * N;
stack<int>data;
vector<int>dataPre, dataOrd;
while (N--)
{
string str;
cin >> str;
if (str == "Push")
{
int a;
cin >> a;
dataPre.push_back(a);
data.push(a);
}
else
{
dataOrd.push_back(data.top());
data.pop();
}
}
Node* root;
root = Create(dataPre, dataOrd, , dataPre.size() - , , dataOrd.size() - );
vector<int>res;
LastTravle(res, root);
for (int i = ; i < res.size() - ; ++i)
cout << res[i] << " ";
cout << res[res.size() - ] << endl;
}
版本二:
使用key,避免了重复数字的尴尬,也不需真正构造一颗二叉树
#include <vector>
#include <stack>
#include <string>
using namespace std;
vector<int> pre, in, post, value;
void postorder(int root, int start, int end) {
if (start > end) return;
int i = start;
while (i < end && in[i] != pre[root]) i++;
postorder(root + , start, i - );
postorder(root + + i - start, i + , end);
post.push_back(pre[root]);
}
int main() {
int n;
cin >> n;
n = * n;
stack<int> s;
int key = ;
while (n--) {
string str;
cin >> str;
if (str.length() == ) {
int num;
cin >> num;
value.push_back(num);
pre.push_back(key);
s.push(key++);
}
else {
in.push_back(s.top());
s.pop();
}
}
postorder(, , pre.size() - );
printf("%d", value[post[]]);
for (int i = ; i < n; i++)
printf(" %d", value[post[i]]);
return ;
}
PAT甲级——【牛客练习A1004】的更多相关文章
- PAT甲级训练刷题代码记录
刷题链接:https://www.patest.cn/contests/pat-a-practise 1001 #include <iostream> #include <stdio ...
- PAT甲级题解(慢慢刷中)
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~http://www.cnblogs.com/chenxiwenruo/p/6102219.html特别不喜欢那些随便转载别人的原创文章又不给 ...
- PAT甲级1131. Subway Map
PAT甲级1131. Subway Map 题意: 在大城市,地铁系统对访客总是看起来很复杂.给你一些感觉,下图显示了北京地铁的地图.现在你应该帮助人们掌握你的电脑技能!鉴于您的用户的起始位置,您的任 ...
- PAT——甲级1009:Product of Polynomials;乙级1041:考试座位号;乙级1004:成绩排名
题目 1009 Product of Polynomials (25 point(s)) This time, you are supposed to find A×B where A and B a ...
- PAT——甲级1046S:shortest Distance
这道题,折磨了我一个多小时,前前后后写了三个算法. 1046 Shortest Distance (20 point(s)) The task is really simple: given N ex ...
- PAT甲级考前整理(2019年3月备考)之三,持续更新中.....
PAT甲级考前整理一:https://www.cnblogs.com/jlyg/p/7525244.html,主要讲了131题的易错题及坑点 PAT甲级考前整理二:https://www.cnblog ...
- PAT甲级满分攻略|记一次考试经历
一次考试经历 今天是"大雪",很冷. 来到隔壁的学校考试,记得上一次来河中医是两年前大一刚开学吧,那天晚上印象比较深刻,6个室友骑车到处闲逛.当时还不会Hello world. 很 ...
- 图论 - PAT甲级 1013 Battle Over Cities C++
PAT甲级 1013 Battle Over Cities C++ It is vitally important to have all the cities connected by highwa ...
- 图论 - PAT甲级 1003 Emergency C++
PAT甲级 1003 Emergency C++ As an emergency rescue team leader of a city, you are given a special map o ...
随机推荐
- shell 通配符,管道符,输入/输出重定向,命令置换
1. echo 输出 [echo 输出的内容 ]把内容输出到终端上 如果字符串使用双引号,echo命令原样输出 [ echo "hello world" ] ...
- Java迷宫代码,深度优先遍历
此次迷宫深度优先遍历寻找路径采用栈结构,每个节点都有固定的行走方向(右下左上),除非一个方向走不通,不然会一条道走到黑. 如果路径存在,打印出行走路径,否则打印出迷宫不存在有效路径. 方向常量定义: ...
- div中内容可左右上下滑动
在<table>外套一层<div>,并且声明overflow:scroll属性,如: <div style="width:1620px;height:680px ...
- 深入解读阿里云数据库POLARDB核心功能会话读一致性
POLARDB架构 我们知道,POLARDB是一个由多个节点构成的数据库集群,一个主节点,多个读节点.对外默认提供两个地址,一个是集群地址,一个是主地址,推荐使用集群地址,因为它具备读写分离功能可以把 ...
- 11 Python Libraries You Might Not Know
11 Python Libraries You Might Not Know by Greg | January 20, 2015 There are tons of Python packages ...
- day 56 Django基础五之django模型层(二)多表操作
Django基础五之django模型层(二)多表操作 本节目录 一 创建模型 二 添加表记录 三 基于对象的跨表查询 四 基于双下划线的跨表查询 五 聚合查询.分组查询.F查询和Q查询 六 ORM ...
- PAT甲级——A1139 First Contact【30】
Unlike in nowadays, the way that boys and girls expressing their feelings of love was quite subtle i ...
- Quartz 定时任务配置(spring中)
<!-- Quartz --> <bean name="task" class="com.geostar.geosmarter.nodemanag ...
- C# 反射的委托创建器
原文:C# 反射的委托创建器 .Net 的反射是个很好很强大的东西,不过它的效率却实在是不给力.已经有很多人针对这个问题讨论过了,包括各种各样的 DynamicMethod 和各种各样的效率测试,不过 ...
- visual studio 2017--括号自动补全
---恢复内容开始--- 平常在visual studio中编写C++代码,一般括号之类的都是自动补全的,最近想用来编写Python,发现括号不能补全了,很不适应. Python编写时好像括号好像默认 ...