题意分析:

给出一个1000以内的整数N,以及N个整数,并且这N个数是按照完全二叉树的层序遍历输出的序列,输出所有的整条的先序遍历的序列(根 右 左),以及判断整棵树是否是符合堆排序的规则(判断是大顶堆,小顶堆,不是堆)

题解分析:

由于给出的整数序列是按照完全二叉树的层序遍历,所以不存在中间有空的节点,并且层序遍历满足1~N的节点顺序正好方便我们一边输入一边建立完全二叉树,之后就是正常的先序遍历(这题要求根 右 左),有所区别的是最后的输出每次到达最后一个叶子节点的时候都需要输出一遍整条序列,所以我们用一个vector存储遍历的路径,关于是否是大顶堆小顶堆的判断其实就是比较每次的一条路径,如果所有的路径都是递增就是小顶堆,都是递减就是大顶堆,无论是同一条路径中出现了乱序还是有两条路径的排序规则不同都将导致堆的不存在,还有就是开范围的时候多开一倍不然就会越界哟~

代码:

  1. 1 #include<iostream>
  2. 2 #include<stdio.h>
  3. 3 #include<vector>
  4. 4 #include<string.h>
  5. 5 using namespace std;
  6. 6
  7. 7 const int N = 2005;
  8. 8 vector<int> road; //可能有相同大小的键值的节点
  9. 9 int tree[N];
  10. 10 int n, flag, appear;
  11. 11
  12. 12 void pre(int gen){
  13. 13 road.push_back(tree[gen]);
  14. 14 if(tree[gen*2] == 0 && tree[gen*2+1] == 0){
  15. 15 int up = 0; int down = 0;
  16. 16 for(int i = 0; i < road.size(); i++){
  17. 17 if(i != 0) printf(" ");
  18. 18 printf("%d", road[i]);
  19. 19 if(i > 0 && road[i] > road[i-1]) up = 1; //出现递增
  20. 20 if(i > 0 && road[i] < road[i-1]) down = 1; //出现递降
  21. 21 }
  22. 22 if(appear == 0){
  23. 23 if(up == 1 && down == 0){
  24. 24 appear = 1; flag = -1;
  25. 25 }
  26. 26 if(up == 0 && down == 1){
  27. 27 appear = 1; flag = 1;
  28. 28 }
  29. 29 if(up == 1 && down == 1){
  30. 30 appear = 1; flag = 0;
  31. 31 }
  32. 32 }else{
  33. 33 if(flag == 1 && up == 1) flag = 0;
  34. 34 if(flag == -1 && down == 1) flag = 0;
  35. 35 }
  36. 36 printf("\n");
  37. 37 road.pop_back();
  38. 38 return;
  39. 39 }
  40. 40 if(tree[gen*2+1] != 0) pre(gen*2+1);
  41. 41 if(tree[gen*2] != 0) pre(gen*2);
  42. 42 road.pop_back();
  43. 43 }
  44. 44
  45. 45 int main(){
  46. 46 scanf("%d", &n);
  47. 47 memset(tree, 0, sizeof(tree));
  48. 48 for(int i = 1; i <= n; i++) scanf("%d", &tree[i]);
  49. 49 appear = 0; //是否和flag比较过 0没有 1比较过
  50. 50 flag = 0; //1为大顶堆 -1为小顶堆 0为都不是
  51. 51 pre(1);
  52. 52 if(flag == 0) printf("Not Heap\n");
  53. 53 if(flag == 1) printf("Max Heap\n");
  54. 54 if(flag == -1) printf("Min Heap\n");
  55. 55 return 0;
  56. 56 }

PAT甲级 1155 Heap Paths (30分) 堆模拟的更多相关文章

  1. PAT Advanced 1155 Heap Paths (30 分)

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

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

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

  3. PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]

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

  4. PAT 甲级 1155 Heap Paths

    https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, ...

  5. PAT 甲级 1080 Graduate Admission (30 分) (简单,结构体排序模拟)

    1080 Graduate Admission (30 分)   It is said that in 2011, there are about 100 graduate schools ready ...

  6. PAT 甲级 1072 Gas Station (30 分)(dijstra)

    1072 Gas Station (30 分)   A gas station has to be built at such a location that the minimum distance ...

  7. PAT 甲级 1049 Counting Ones (30 分)(找规律,较难,想到了一点但没有深入考虑嫌麻烦)***

    1049 Counting Ones (30 分)   The task is simple: given any positive integer N, you are supposed to co ...

  8. PAT 甲级 1030 Travel Plan (30 分)(dijstra,较简单,但要注意是从0到n-1)

    1030 Travel Plan (30 分)   A traveler's map gives the distances between cities along the highways, to ...

  9. PAT 甲级 1026 Table Tennis (30 分)(坑点很多,逻辑较复杂,做了1天)

    1026 Table Tennis (30 分)   A table tennis club has N tables available to the public. The tables are ...

随机推荐

  1. SpringBoot + Layui +Mybatis-plus实现简单后台管理系统(内置安全过滤器)

    1. 简介   layui(谐音:类UI)是一款采用自身模块规范编写的前端UI框架,遵循原生HTML/CSS/JS的书写与组织形式,门槛极低,拿来即用.其外在极简,却又不失饱满的内在,体积轻盈,组件丰 ...

  2. Flink批处理读取Hive写入MySql

    把hive 表stu77 的数据写入 mysql 表test_stu 中. 中间可以加自己的逻辑. import org.apache.flink.table.api.EnvironmentSetti ...

  3. BloomFilter中保存的数据量

    结果 /** * @author WeiJiQian * BF_CARDINAL_THRESHOLD BF_FALSE_POSITIVE_RATE 保存的数据量 * 100,0000 0.01 391 ...

  4. spark踩坑--WARN ProcfsMetricsGetter: Exception when trying to compute pagesize的最全解法

    spark踩坑--WARN ProcfsMetricsGetter: Exception when trying to compute pagesize的最全解法 问题描述 大概是今年上半年的时候装了 ...

  5. 网络编程-python实现-socket(1.1.1)

    @ 目录 1.不同电脑进程之间如何通信 2.什么是socket 3.创建socket 1.不同电脑进程之间如何通信 利用ip地址 协议 端口 标识网络的进程,网络中的进程通信就可以利用这个标志与其他进 ...

  6. 【electron-playground系列】打包优化之路

    作者:梁棒棒 简介 electron打包工具有两个:electron-builder,electron-packager,官方还提到electron-forge,其实它不是一个打包工具,而是一个类似于 ...

  7. C#中的深度学习(三):理解神经网络结构

    在这篇文章中,我们将回顾监督机器学习的基础知识,以及训练和验证阶段包括哪些内容. 在这里,我们将为不了解AI的读者介绍机器学习(ML)的基础知识,并且我们将描述在监督机器学习模型中的训练和验证步骤. ...

  8. Python实现多个pdf文件合并

    背景 由于工作原因,经常需要将多个pdf文件合并后打印,有时候上网找免费合并工具比较麻烦(公司内网不能访问公网),于是决定搞个小工具. 具体实现 需要安装 PyPDF2 pip install PyP ...

  9. (六)、mkdir--创建目录make directory

    一.命令详解与命令格式 在文件系统中创建新的目录, 格式:mkdir   [-选项]   目录名 目录名既可以是相对路径名,也可是相对路径名 选项: -p或者--parent,创建指定路径中所有不存在 ...

  10. redis scan 命令指南

    redis scan 命令指南 1. 模糊查询键值 redis 中模糊查询key有 keys,scan等,一下是一些具体用法. -- 命令用法:keys [pattern] keys name* -- ...