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. Linux—— 报错汇总

    前言 记录Linux相关的错误问题和解决方法 问题 tar: Error is not recoverable: exiting now [报错] tar -zxvf mysql-server_5.6 ...

  2. Helm 安装使用

    简介 很多人都使用过Ubuntu下的ap-get或者CentOS下的yum, 这两者都是Linux系统下的包管理工具.采用apt-get/yum,应用开发者可以管理应用包之间的依赖关系,发布应用:用户 ...

  3. xpath用发

    xpath的更多语法: https://docs.microsoft.com/zh-cn/previous-versions/dotnet/netframework-2.0/ms256039(v=vs ...

  4. redux沉思录:基于flux、状态管理、函数式编程的前端状态管理框架

    基于flux和reduce的通信和状态管理机制; 和数据库管理系统一样,redux是一个状态管理系统(或机制). const store = createStore( reducer, compose ...

  5. str2int HDU - 4436 (后缀自动机)

    str2int \[ Time Limit: 3000 ms\quad Memory Limit: 131072 kB \] 题意 给出 \(n\) 个串,求出这 \(n\) 个串所有子串代表的数字的 ...

  6. 【CF55D】Beautiful numbers

    [CF55D]Beautiful numbers 题面 洛谷 题解 考虑到如果一个数整除所有数那么可以整除他们的\(lcm\),而如果数\(x\)满足\(x\bmod Lcm(1,2...,9)=r\ ...

  7. request和response文件下载案例

    一.需求分析 * 文件下载需求: 1. 页面显示超链接 2. 点击超链接后弹出下载提示框 3. 完成图片文件下载 * 分析: 1. 超链接指向的资源如果能够被浏览器解析,则在浏览器中展示,如果不能解析 ...

  8. OpenFOAM——圆筒壁稳态导热

    对于圆筒壁的稳态导热,温度分布的解析解为: IN为恒温边界,设置为300K,OUT也为恒温边界,设置为500K 固体导热系数为:0.0887W/(m·K) 首先进行建模操作,任何建模软件均可,本算例采 ...

  9. c++笔试题 已迁移完成

    转载 1.C和C++的特点与区别? 答:(1)C语言特点:1.作为一种面向过程的结构化语言,易于调试和维护: 2.表现能力和处理能力极强,可以直接访问内存的物理地址: 3.C语言实现了对硬件的编程操作 ...

  10. 如何保证MQ的顺序性?比如Kafka

    三.如何保证消息的顺序性 1. rabbitmq 拆分多个queue,每个queue一个consumer,就是多一些queue而已,确实是麻烦点:或者就一个queue但是对应一个consumer,然后 ...