1155 Heap Paths (30 分)( BST+栈 )

PAT 1155 Heap Paths (30 分)- 甲级 In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (i... 阅读详情

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) the key of C. A common implementation of a heap is the binary heap, in which the tree is a complete binary tree. (Quoted from Wikipedia at https://en.wikipedia.org/wiki/Heap_(data_structure))

One thing for sure is that all the keys along any path from the root to a leaf in a max/min heap must be in non-increasing/non-decreasing order.

Your job is to check every path in a given complete binary tree, in order to tell if it is a heap or not.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (1<N≤1,000), the number of keys in the tree. Then the next line contains N distinct integer keys (all in the range of int), which gives the level order traversal sequence of a complete binary tree.

Output Specification:

For each given tree, first print all the paths from the root to the leaves. Each path occupies a line, with all the numbers separated by a space, and no extra space at the beginning or the end of the line. The paths must be printed in the following order: for each node in the tree, all the paths in its right subtree must be printed before those in its left subtree.

Finally print in a line Max Heap if it is a max heap, or Min Heap for a min heap, or Not Heap if it is not a heap at all.

Sample Input 1:

8
98 72 86 60 65 12 23 50

Sample Output 1:

98 86 23
98 86 12
98 72 65
98 72 60 50
Max Heap

Sample Input 2:

8
8 38 25 58 52 82 70 60

Sample Output 2:

8 25 70
8 25 82
8 38 52
8 38 58 60
Min Heap

Sample Input 3:

8
10 28 15 12 34 9 8 56

Sample Output 3:

10 15 8
10 15 9
10 28 34
10 28 12 56
Not Heap

从右向左建树,判断从根节点到每一个叶结点的路径是否都满足>=或<=

路径拿栈记录,递归往上返回一次退栈一个元素,递归碰到叶结点时输出栈中内容

注意递归多个出口时一定要把判断叶结点为空这个条件放在最开始!

代码:

#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string>
#include <string.h>
#include <vector>
#include <stack>
#include <queue>
#include <map>

using namespace std;

int res[1002];
int tail = 0,rear = -1;
int ans[1002];
int ansRear = 0;
int tans;
typedef struct Node{
    struct Node* left_ptr;
    struct Node* right_ptr;
    int index;
    int data;
}BinNode,*BinTree;

void newNode(BinTree *node, int data, int index){
    *node = (BinTree)malloc(sizeof(BinNode));
    (*node)->data = data;
    (*node)->index = index;
    (*node)->left_ptr = NULL;
    (*node)->right_ptr = NULL;
}

void insertNode(BinTree *node, int data, int index){
    if(*node == NULL)
        return;
    if((index/2) == ((*node)->index)){
        if((*node)->right_ptr == NULL)
            newNode(&((*node)->right_ptr), data, index);
        else if((*node)->left_ptr == NULL)
            newNode(&((*node)->left_ptr), data, index);
        return;
    }
    insertNode(&((*node)->right_ptr), data, index);
    insertNode(&((*node)->left_ptr), data, index);
}

int check(){
    int flag = -1;  //1 minheap 2 maxheap 0 notheap
    for(int i = tail; i < rear; i++){
        if(res[i] >= res[i+1])
            continue;
        else{
            flag = 0;
            break;
        }
    }
    if(flag == -1)
        return flag = 2;
    if(flag == 0){
        for(int i = tail; i < rear; i++){
            if(res[i] <= res[i+1])
                continue;
            else
                return flag = 0;
        }
    }
    if(flag == 0)
        return flag = 1;
}

void printRes(){
    for(int i = tail; i < rear; i++)
        cout<<res[i]<<" ";
    cout<<res[rear]<<endl;
    tans = check();
    ans[ansRear++] = tans;
}

void travelTree(BinTree root){
    if(root == NULL)
        return;
    res[++rear] = root->data;
    if((root->left_ptr == NULL)&&(root->right_ptr == NULL)){
        printRes();
        --rear;
        return;
    }
    travelTree(root->left_ptr);
    travelTree(root->right_ptr);
    --rear;
}
int main()
{
    BinTree root = NULL;
    int n,temp;
    cin>>n;
    cin>>temp;
    root = (BinTree)malloc(sizeof(BinNode));
    root->data = temp;
    root->index = 1;
    root->left_ptr = NULL;
    root->right_ptr = NULL;
    for(int i = 2; i <= n; i ++){
        cin>>temp;
        insertNode(&root, temp, i);
    }
    travelTree(root);
    int mark = ans[0];

    for(int i=0;i<ansRear;i++){
        if(ans[i] == 0){
            cout<<"Not Heap";
            return 0;
        }
        if(ans[i] == mark)
            continue;
        else{
            cout<<"Not Heap";
            return 0;
        }
    }
    if(mark == 1)
        cout<<"Min Heap";
    else if(mark == 2)
        cout<<"Max Heap";
    return 0;
}

 

【每天一个知识点】多视图子空间聚类(Multi-view Subspace Clustering) 本文深入解析了多视图子空间聚类技术,这是一种处理多源异构数据的先进方法。它通过融合多个数据视图(如文本、图像、网络结构)的共识与互补信息,学习统一的潜在子空间表示,从而获得比单一视图更鲁棒、准确的聚类结果。文章详细阐述了其核心思想、主流技术流派(如图融合、共同潜在子空间、张量及深度学习方法),并提供了Python实战代码与调优经验,适用于跨模态检索、生物信息学等多领域复杂数据分析。 阅读详情

相关推荐

无感无刷电机三段式启动避坑指南:从预定位到闭环切换的STM32代码实现

本文深入解析无感无刷电机的三段式启动流程,涵盖预定位、启动加速与闭环切换三大阶段。重点对比了恒频升压、恒压升频与升频升压三种核心启动策略的优劣,并提供了基于STM32的详细状态机代码实现与关键参数调试指南,帮助工程师有效规避启动失步、线圈过热等常见问题,实现稳定可靠的无感驱动。

herb5的博客 535

PAT|1155 Heap Paths(堆、递归)

题目大意 在计算机科学中,堆是一种特殊的基于树的数据结构,它满足堆属性:如果 P 是 C 的父节点,则 P 的键(值)要么大于或等于(在最大堆中) ) 或小于或等于(在最小堆中)C 的键。堆的常见实现是二叉堆,其中树是完全二叉树。 (引自维基百科 https://en.wikipedia.org/wiki/Heap_(data_structure)) 可以肯定的是,最大/最小堆中从根到叶的任何路径上的所有键都必须按非递增/非递减顺序排列。 你的工作是检查给定的完整二叉树中的每条路径,以确定它是否是一个堆。

qq_43528044的博客 233

DIY一个SM2262ENG 2TB Nvme固态硬盘,慧荣SM2262EN主控

网友订做的 材料: 慧荣科技的62en,2片1G 三星DDR4缓存,4颗PF29F04T2AOCTH1,8CE B17A。SM2262EN属于慧荣第二代PCIe SSD主控,支持PCIe G3×4通道和全新的NVMe 1.3规范、8个NAND通道设计,并提供完整固件设计,最大循序读取速度高达3.5GB/s,循序写入速度达3.0GB/s,随机读写则高达420K IOPS和420K IOPS。此外,还采用目前最先进的低功耗设计,无论是在待机还是在工作状态下,均展现超低功耗,有效减少30%的功耗,满足用户追求超

m0_55207244的博客 1万+

PAT A1155 Heap Paths (30 分)

PAT A1155 Heap Paths (30 分)

zhengqinyuanbift的博客 248

Linux下安装FastDFS及生成缩略图

1. 安装编译环境 yum install git gcc gcc-c++ make automake vim wget libevent -y 2. 安装libfastcommon基础库 mkdir /root/fastdfs cd /root/fastdfs git clone https://github.com/happyfish100/libfastcommon.git --depth 1 cd libfastcommon/ ./make.sh && ./make.sh ins

weixin_45234710的博客 1374

ROS与UR10e机械臂实战:从零搭建到精准控制

本文详细介绍了如何从零开始搭建ROS与UR10e机械臂的开发环境,包括硬件准备、软件配置、网络设置以及MoveIt!运动控制实战。通过具体的代码示例和常见问题排查,帮助开发者快速掌握UR10e机械臂的精准控制技术,适用于工业自动化和实验室研究场景。

m3n5b7v8c9x的博客 772

1147 Heaps

题目 题意:给定完全二叉树的层序遍历,问该二叉树是否为堆。为什么堆,以及输出它的后序遍历 #include<iostream> #include<vector> using namespace std; int n,m; int s[1003]; vector<int>post; bool maxheap() { for(int i=0; i<n...

qq_1300258993的博客 420

MATLAB新手必看:SVPWM七段式算法仿真全流程解析(附完整代码)

本文为MATLAB新手和电机控制初学者提供了SVPWM七段式算法的完整仿真与代码实现指南。文章详细解析了从扇区判断、矢量作用时间计算到PWM波形生成的全流程,并附带了可直接运行的MATLAB代码,帮助读者在理解FOC控制核心算法的基础上,快速掌握工程仿真与调试技巧。

weixin_29074295的博客 281

#1155. Heap Paths【完全二叉树 + 堆 + DFS】

原题链接 Problem Description: In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or

geraltofrivia123的博客 200

MIKE水动力笔记5_建立水动力模型

前文已经讲了如何制作网格文件(.mdf文件),这一博文就讲如何建立水动力模型。

晏长街的博客 3万+

【YoloV11改进策略】基于流形约束超连接(mHC)增强YoloV11全局特征提取能力|独家复现|梁文峰署名论文

论文标题:流形约束超连接 (Manifold-Constrained Hyper-Connections, mHC)核心问题:针对超连接 (Hyper-Connections, HC) 扩展残差流宽度时破坏恒等映射、引发训练不稳定性和高内存开销的问题,提出一种约束性框架。目标:恢复残差连接的稳定性与可扩展性,同时保持HC的性能增益。基准模型:基于DeepSeek-V3的MoE架构,进行了3B、9B、27B模型的实验验证。论文图示mHC 是一个通用的HC扩展框架。

m0_47867638的博客 1349

1155 Heap Paths (30 分)

1155 Heap Paths (30 分) 题目大意 给定完全二叉树层序遍历得到的序列,输出每条叶子结点到根结点的路径(从右向左输出)。 基本思路 判断属于什么堆思路这里就不写了,请看A1147的思路。这里主要说明一下如何从右向左输出每一条路径: 定义一个数组depthc存放每个叶子结点的层次,index代表最高高度和第二高高度的临界叶子结点的编号,函数solve用来求路径(逆序),cnt存放当前是第几条路径,vector path[maxn]存放第cnt条路径的所有结点(逆序存放) 求出每个叶子节点的

Baifeili的博客 186

osg点云加载与渲染

osg点云加载与渲染

cangqiongxiaoye的博客 821

常用算法代码模板与题单 | D - 图论(更新于2026.10.7)

这篇文档其实是我自己学习路上的一份“复习笔记”。之前看了不少教程,零零散散记了一堆代码片段和小技巧,时间久了就容易忘,翻起来也麻烦。所以干脆花时间整理了一下,把常用的模板、容易踩的坑、还有一些个人觉得挺实用的小技巧都归到了一起。 如果你也在学类似的东西,或许能帮你省下一些翻找的时间;如果有哪里写得不清楚或不对,也欢迎随时指正交流。 希望这份整理也能对你有一点点帮助~ ✨

团爸讲算法 788

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

1155 Heap Paths

题目来源:PAT (Advanced Level) Practice In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or les

weixin_51032998的博客 166

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

PAT甲级 1155 Heap Paths (30 分)

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t

DoMoreSpeakLess的博客 201

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

1155 Heap Paths (30 分)(构建树)

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t

weixin_45962741的博客 249

PTA甲级 1155 Heap Paths (C++)

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (in a max heap) or less than or equal to (in a min heap) t

Misdirection 218

PAT A1147 2019.08.12 【层序完全二叉树性质建树 堆 后序遍历】

1147Heaps(30分) In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater ...

一个非计算机专业的 博客 269

1037 Magic Coupon (25 分)(段错误原因)

The magic shop in Mars is offering some magic coupons. Each coupon has an integer N printed on it, meaning that when you use this coupon with a product, you may get N times the value of that product back! What is more, the shop also offers some bonus produ

weixin_45962741的博客 495

【PAT甲级】1155 Heap Paths(给定完全二叉树的层序遍历,判断是否为堆及堆的类型)

dfs每次插入一个当前点,检测到当前节点为叶子节点(u * 2 > n)时输出nodes中的全部元素,每次弹出nodes中的最后一个元素还原现场。判断类型用lt(less than)和gt(greater than)两个变量,每次输出nodes时顺带判断nodes当前元素较上一元素的变换情况。最后如果gt和lt都为true,说明路径的序列有递增也有递减,则不是堆,若只有gt为true,则说明路径是递增的,则为小根堆,否则为大根堆。 #include <bits/stdc++.h> usi

Ybibabo__的博客 1465

1155 Heap Paths (30 分)

1155 Heap Paths (30 分) In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either gre...

galesaur_wcy 750

1155 Heap Paths (30 point(s))

In computer science, a heap is a specialized tree-based data structure that satisfies the heap property: if P is a parent node of C, then the key (the value) of P is either greater than or equal to (i...

zclimou的博客 440

PAT-ADVANCED1155——Heap Paths

我的PAT-ADVANCED代码仓:https://github.com/617076674/PAT-ADVANCED 原题链接:https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 题目描述: 题目翻译: 1155 堆路径 在计算机科学中,堆是一种专门的基于树的数据结构,它满足堆...

清风阁 496
上一篇: MySQL 内连接、外连接、左连接、右连接、全连接
下一篇: JAVA IO(1) Reader与Writer
donCoder
博客等级 码龄9年 65粉丝 79原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值