7-4 Cartesian Tree (30分)
 

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层序)的更多相关文章

  1. 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 ...

  2. PAT甲级:1064 Complete Binary Search Tree (30分)

    PAT甲级:1064 Complete Binary Search Tree (30分) 题干 A Binary Search Tree (BST) is recursively defined as ...

  3. 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 ...

  4. 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 ...

  5. 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 ( ...

  6. pat甲级 1155 Heap Paths (30 分)

    In computer science, a heap is a specialized tree-based data structure that satisfies the heap prope ...

  7. PAT甲级——1131 Subway Map (30 分)

    可以转到我的CSDN查看同样的文章https://blog.csdn.net/weixin_44385565/article/details/89003683 1131 Subway Map (30  ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. dfs 全排列 使用交换——含重复元素和非重复元素

    15. 全排列 中文 English 给定一个数字列表,返回其所有可能的排列. 样例 样例 1: 输入:[1] 输出: [ [1] ] 样例 2: 输入:[1,2,3] 输出: [ [1,2,3], ...

  2. 201671030122 杨凡亿 实验十四 团队项目评审&课程学习总结

    项目 内容 课程名称 2016级计算机科学与工程学院软件工程(西北师范大学) 作业要求 实验十四 团队项目评审&课程学习总结 课程学习目标 (1)掌握软件项目评审会流程(2)反思总结课程学习内 ...

  3. 项目Beta冲刺--3/7

    项目Beta冲刺--3/7 作业要求 这个作业属于哪个课程 软件工程1916-W(福州大学) 这个作业要求在哪里 项目Beta冲刺 团队名称 基于云的胜利冲锋队 项目名称 云评:高校学生成绩综合评估及 ...

  4. 记录一次群答问:jmeter正则提取器轻松提取一个及多个值

    图截得比较完整,电脑端浏览器放大倍数看吧^_^,手机端可以点击图片然后放大看. 一个正则提取问题 前几天,在Q群和微信群里被同时@,咨询这样一个问题:服务器返回:name="tom" ...

  5. 实时查看mysql当前连接数

    如何实时查看mysql当前连接数? 1.查看当前所有连接的详细资料:./mysqladmin -uadmin -p -h10.140.1.1 processlist 2.只查看当前连接数(Thread ...

  6. SQL Server 定期归档大表历史数据

    很少有开发会考虑到数据归档的问题已经数据增长的问题,当程序运行一段时间后,就会出现各种问题,部分问题可以修改SQL语句或使用索引来解决,但如果SQL语句无法修改,糟糕的SQL语句无法使用索引,归档历史 ...

  7. presto-gateway lyft 团队开源的prestodb 的负载均衡、代理、网关工具

    presto-gateway 是 lyft 团队开源 的prestodb 的工具,很方便,我们可以用来方便的管理presto 多集群 通过yaml 进行配置管理,可以方便的管理不同的集群 lyft 参 ...

  8. 洛谷 P1099 树网的核+P2491 [SDOI2011]消防

    写在前面:由于是双倍经验就放一块了,虽然数据范围差的有点大. 题目链接 题意:在树的直径上选择一条长度不超过s的路径使这条路径上的点到树上任意点的最大距离最小. 这题数据好像非常水,我写了上界n^2不 ...

  9. web前端开发高级

    前端高效开发框架技术与应用 Vue 基础Vue 框架简介 MVX 模式介绍Vue 框架概述如何使用 Vue.js 基础语法 实例对象生命周期模板语法计算属性Methods 方法 渲染 列表渲染条件渲染 ...

  10. nginx之TCP反向代理

    实现Nginx tcp负载均衡 Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似 ...