题意分析:

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

题解分析:

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

代码:

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

    Description 现有一台预测机,可以预测当前 \(n\) 个人在 \(T\) 个时刻内的生死关系.关系有两种: \(\texttt{0 t x y}\):如果 \(t\) 时刻 \(x\) 死 ...

  2. WebService-问题

    1.引用问题 在用C#对接webservice的时候,常用的方法是下载vs中引用webservice的地址.然后,new对应的client就可以使用了.但在,实际应用中往往会遇到webservice访 ...

  3. Linux 查看CPU型号,内存大小,硬盘空间的命令

    1 查看CPU 1.1 查看CPU个数 # cat /proc/cpuinfo | grep "physical id" | uniq | wc -l 1.2 查看CPU核数 # ...

  4. Jenkins的war包安装

    安装Jenkins首先要安装jdk,在官网下载jdk安装并配置环境变量 1.Jenkins下载地址,下载war包 https://www.jenkins.io/download/ 2.打开命令行窗口, ...

  5. Docker(三):Docker安装MySQL

    查找MySQL镜像 镜像仓库 https://hub.docker.com/ 下拉镜像 docker pull mysql:5.7 查看镜像 docker images 创建MySQL容器 命令式启动 ...

  6. ssh 免密码登陆设置不成功

    记一次centos6设置免密码登陆设置不成功的解决.自己挖的坑自己填. ssh 免密码登陆设置( 正常情况下是这样的,设置成功后登陆主机是不需要密码的) [root@master .ssh]# ssh ...

  7. css 11-CSS3属性详解(一)

    11-CSS3属性详解(一) #前言 我们在上一篇文章中学习了CSS3的选择器,本文来学一下CSS3的一些属性. 本文主要内容: 文本 盒模型中的 box-sizing 属性 处理兼容性问题:私有前缀 ...

  8. vue第十七单元(电商项目逻辑处理,电商划分)

    第十七单元(电商项目逻辑处理,电商划分) #课程目标 1.什么是电商项目 2.什么是B2B,B2C,C2C模式,常见的电商项目 3.移动端电商项目常见的逻辑处理 4.[知识扩展]传统系统架构及分布式系 ...

  9. 服务器安装PVE6.1.2

    1     去官网下载PVE的镜像文件 https://www.proxmox.com/en/downloads 2     制作成U盘启动 准备一个U盘用来制作启动盘(记得将U盘里原来的资料备份然后 ...

  10. C#中 Thread,Task,Async/Await 异步编程

    什么是异步 同步和异步主要用于修饰方法.当一个方法被调用时,调用者需要等待该方法执行完毕并返回才能继续执行,我们称这个方法是同步方法:当一个方法被调用时立即返回,并获取一个线程执行该方法内部的业务,调 ...