PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)
A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesian tree is shown by the figure.

Your job is to output the level-order traversal sequence of the min-heap Cartesian tree.
Input Specification:
Each input file contains one test case. Each case starts from giving a positive integer N (≤), and then N distinct numbers in the next line, separated by a space. All the numbers are in the range of int.
Output Specification:
For each test case, print in a line the level-order traversal sequence of the min-heap Cartesian tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.
Sample Input:
10
8 15 3 4 1 5 12 10 18 6
Sample Output:
1 3 5 8 4 6 15 10 12 18
题意:
给一个最小堆的中序遍历,求层序遍历。
题解:
也不难,最朴素稳妥的方法就是老老实实建树,老老实实bfs层序遍历。最小堆的叶节点总是小于等于根节点,所以每次都挑出最小的值最为根,然后左子树右子树重复上述过程即可。

AC代码:
#include<bits/stdc++.h>
using namespace std;
int a[],b[];
int n;
struct node{
int d;
node *left,*right;
};
queue<node*>q;
vector<int>v;
node* build(int l,int r){
if(l>r) return NULL;
node *root=new node();
int pos=-,k=;//找到最小的
for(int i=l;i<=r;i++){
if(k>a[i]){
k=a[i];
pos=i;
}
}
root->d=a[pos];
root->left=build(l,pos-);//左子树
root->right=build(pos+,r);//右子树
return root;
}
int main(){
cin>>n;
for(int i=;i<=n;i++) cin>>a[i];
node *root=build(,n);//建树
q.push(root);//bfs层序遍历
while(!q.empty()){
node *tree=q.front();
q.pop();
v.push_back(tree->d);
if(tree->left) q.push(tree->left);
if(tree->right) q.push(tree->right);
}
for(int i=;i<v.size();i++){//输出
cout<<v.at(i);
if(i!=v.size()-) cout<<" ";
}
return ;
}
PAT-2019年冬季考试-甲级 7-4 Cartesian Tree (30分)(最小堆的中序遍历求层序遍历,递归建树bfs层序)的更多相关文章
- PAT-2019年冬季考试-甲级 7-1 Good in C (20分)
7-1 Good in C (20分) When your interviewer asks you to write "Hello World" using C, can y ...
- PAT甲级:1064 Complete Binary Search Tree (30分)
PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...
- PAT (Advanced Level) Practise - 1099. Build A Binary Search Tree (30)
http://www.patest.cn/contests/pat-a-practise/1099 A Binary Search Tree (BST) is recursively defined ...
- PAT-2019年冬季考试-甲级 7-3 Summit (25分) (邻接矩阵存储,直接暴力)
7-3 Summit (25分) A summit (峰会) is a meeting of heads of state or government. Arranging the rest ar ...
- PAT-2019年冬季考试-甲级 7-2 Block Reversing (25分) (链表转置)
7-2 Block Reversing (25分) Given a singly linked list L. Let us consider every K nodes as a block ( ...
- pat甲级 1155 Heap Paths (30 分)
In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...
- PAT甲级——1131 Subway Map (30 分)
可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30 ...
- PAT 甲级 1076 Forwards on Weibo (30分)(bfs较简单)
1076 Forwards on Weibo (30分) Weibo is known as the Chinese version of Twitter. One user on Weibo m ...
- PAT 甲级 1068 Find More Coins (30 分) (dp,01背包问题记录最佳选择方案)***
1068 Find More Coins (30 分) Eva loves to collect coins from all over the universe, including some ...
随机推荐
- RabbitMQ 部署记录
1. erlang与rabbitmq版本对应关系: https://www.rabbitmq.com/which-erlang.html 2. 安装erlang 下载地址:http://www.erl ...
- 项目Beta冲刺--4/7
项目Beta冲刺--4/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...
- GitHub & GitHub Desktop能帮我们做什么
GitHub: 1.代码版本管理 GitHub Desktop:
- vscode代码折叠方法
最近换用了vscode代码编辑器,在查看c源码的时候想折叠所有区域的代码,不知道快捷键是哪一个?查看了使用说明,快捷键如下: 1. 折叠所有区域代码的快捷: ctrl + k ctrl + ...
- 小程序&app 注册登录、绑定
前段时间开发中的一款产品,有小程序和app:小程序直接微信登录,app使用手机号+验证码注册,手机号+验证码/密码登录. 用户使用其中一套账号密码即可正常使用,不强制要求完善另一套账号.为避免同一用户 ...
- presto-gateway nodejs client
目前已经有了好几个presto nodejs 的client,为了方便presto-gateway 的连接,修改了一个现有的nodejs client 可以方便的连接presto-gateway 原理 ...
- Vuejs简介
一.网站交互方式 ①传统的开发方式:PHP 中,页面和服务端糅合在一起,在这种项目中服务端占比更重,因为绝大多数都服务端技术,绝大多数网站都是这样的方式 ②前后端分离方式:服务端只处理数据(不关心页面 ...
- 用于C#的极速序列化/反序列工具 MessagePack
MessagePack 比MsgPack-Cli快10倍,并且优于其他C#序列化器.MessagePack for C#还内置了对LZ4压缩的支持 - 一种极快的压缩算法.对于性能追求很重要,特别是在 ...
- 5-(微信小程序篇)关于WiFi模块配网以后利用小程序绑定设备,绑定方式说明
https://www.cnblogs.com/yangfengwu/p/11625189.html 众所周知:使用微信Airkiss 只能给设备配网,并不能够获取设备的MAC地址信息,但是我在 ht ...
- MySQL8.0忘记密码后重置密码(亲测有效)
实测,在mysql8系统下,用mysqld --console --skip-grant-tables --shared-memory可以无密码启动服务 服务启动后,以空密码登入系统 mysql.ex ...