PAT甲级 - 1155 Heap Paths (30 分) ( 完全二叉树 + dfs )

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... 阅读详情
题目链接:点击进入
题目

在这里插入图片描述
在这里插入图片描述

题意

大概就是给一棵完全二叉树,然后让你输出从根节点到所有叶节点的路径,从右往左输出。然后判断该树是大顶堆、小顶堆或者啥也不是。

思路

输出所有根节点到叶子节点的路径,可以将所有叶子节点存下来(先存右边的,这样输出就是从右边开始的了),有几个叶子节点,就输出几条路径。用vector保存每条路上的节点。由于完全二叉树的特点,根节点到叶子节点的路径,就是从叶子节点的编号,不断/2,直到等于1(根节点)的过程中,编号的所有取值。(大概可以理解为,每个点都由它的父亲节点走来,它的父亲节点编号=儿子节点编号/2)
通过push和pop回溯,维护路径,index <= n是对只有左叶节点没有右叶节点的点特判
2.判断是否为堆:从第二个节点开始遍历,如果比父节点小,就不是小顶堆,如果比父节点大,就不是大顶堆~

代码
#include<iostream>
#include<map>
#include<vector>
#include<cstring>
using namespace std;
const int maxn=1e5+10;

int n,a[1010],flag;
vector<int>v,path[1010];

void dfs(int p)
{
	if(p*2>n) 
	{
		v.push_back(p);
		return ;
	}
	if(p*2+1<=n) dfs(p<<1|1);
	if(p*2<=n) dfs(p<<1);
}

void dfs1(int p)
{
	int l=p<<1;
	int r=p<<1|1;
	if(l>n&&r>n) return ;
	if(l<=n&&r<=n)
	{
		if(a[l]>a[p]||a[r]>a[p])
		{
			flag=1;
			return ;
		}
		dfs1(l);
		dfs1(r);
	}
	else if(l<=n)
	{
		if(a[l]>a[p])
		{
			flag=1;
			return ;
		}
		dfs1(l);
	}
	else if(r<=n)
	{
		if(a[r]>a[p])
		{
			flag=1;
			return ;
		}
		dfs1(r);
	}
}

void dfs2(int p)
{
	int l=p<<1;
	int r=p<<1|1;
	if(l>n&&r>n) return ;
	if(l<=n&&r<=n)
	{
		if(a[l]<a[p]||a[r]<a[p])
		{
			flag=1;
			return ;
		}
		dfs2(l);
		dfs2(r);
	}
	else if(l<=n)
	{
		if(a[l]<a[p])
		{
			flag=1;
			return ;
		}
		dfs2(l);
	}
	else if(r<=n)
	{
		if(a[r]<a[p])
		{
			flag=1;
			return ;
		}
		dfs2(r);
	}
}

int main( )
{
	cin>>n;
	for(int i=1;i<=n;i++) cin>>a[i];
	dfs(1);
	for(int i=0;i<v.size();i++)
	{
		int p=v[i];
		path[i].push_back(p);
		while(p/2)
			p/=2,path[i].push_back(p);
	}
	for(int i=0;i<v.size();i++)
	{
		int len=path[i].size();
		for(int j=len-1;j>=0;j--)
		{
			if(j==0)
				cout<<a[path[i][j]]<<endl;
			else 
				cout<<a[path[i][j]]<<' ';
		}
	}
	flag=0;
	dfs1(1);
	if(!flag)
	{
		cout<<"Max Heap"<<endl;
		return 0;
	}
	flag=0;
	dfs2(1);
	if(!flag)
		cout<<"Min Heap"<<endl;
	else
		cout<<"Not Heap"<<endl;
	return 0;
}
18年冬季第四题 PAT甲级 1155 Heap Paths (30分) 题目 题目来源:https://pintia.cn/problem-sets/994805342720868352/problems/1071785408849047552 In computer science, aheapis a specialized tree-based data structure that satisfies the heap property: if P i... 阅读详情

相关推荐

Fluent DPM模型保姆级教程:三通管颗粒流动模拟,从网格导入到轨迹图生成

本文提供Fluent DPM模型在三通管颗粒流动模拟中的保姆级教程,涵盖从网格导入到轨迹图生成的全流程。通过详细步骤和参数设置,帮助用户掌握DPM模型在颗粒流动模拟中的关键技巧,适用于化工、能源等领域的工程应用。

weixin_30615767的博客 579

1155 Heap Paths

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

L_Aster的专栏 482

UART中断接收的实战陷阱:HAL库常见误区与性能调优指南

本文深入解析STM32 HAL库UART中断接收机制,揭示常见实战陷阱如数据溢出、中断优先级配置不当等,并提供性能调优方案。重点介绍如何使用空闲中断处理不定长数据、DMA与中断混合模式优化,以及工业级错误恢复与流量控制策略,帮助开发者构建稳定可靠的串口通信系统。

pea55的博客 747

PAT(甲级)1155

题意:给出一棵完全二叉树,判断它是大根堆还是小根堆或者不是堆,并且要输出所有到叶子节点的路径。 题解:一道30分的题,总体来说不难,掌握二叉树的遍历,对该树遍历两次,第一次判断是大根堆还是小根堆或者不是,第二次遍历输出路径。 注意点:初始点要加到路径中去,注意遍历到边界的问题,如何判断已经访问到叶子节点,以及注意从右儿子开始遍历。 code in view #include<bits/std...

qq_42836930的博客 182

钉钉开发(三)——Java推送钉钉待办任务

之前一篇博文中,讲解了如何推送钉钉群消息,感兴趣的可以点击查看。 钉钉开发(一)——Java给钉钉群推送消息

Monten_Cristo的博客 6410

Redhawk解析PAD / IOPAD的实现方法

Redhawk解析IOPAD的实现方法1. IOPAD DEF的概念说明2. 生成 IOPAD GDS文件3. 生成 IOPAD DEF文件4. Redhawk解析过程中读取IOPAD DEF 本文主要简述【Redhawk解析IOPAD】的实现原理方法,具体脚本没有放在这里。 1. IOPAD DEF的概念说明 2. 生成 IOPAD GDS文件 注: getinfo2IODEF.tcl 是基于innovus获取IOPAD的摆放信息以及库信息。 run_cerate_iopadgds.csh 是基于c

kobayashiyou的博客 3154

PAT甲级1155 Heap Paths (30 分):[C++题解]堆、堆的遍历、树的遍历、dfs输出路径、完全二叉树建树

刷题

阿正的梦工坊 468

具身智能的边缘计算核心构成方案

摘要: 具身智能(Embodied Intelligence)依赖物理实体与环境交互,而边缘计算(Edge Computing)通过本地化数据处理降低延迟、提升实时性。两者结合的核心方案包含五层架构:传感器层采集环境数据,边缘处理单元执行本地计算,通信模块实现低延迟传输,决策引擎运行轻量AI模型,执行器接口转化动作为物理操作。该方案以“边缘优先”为原则,满足毫秒级响应(如延迟<50ms),同时优化能效(功耗降低30%)和隐私保护。适用于机器人、自动驾驶等场景,通过分层闭环设计确保在资源受限环境中的可靠

老郑的博客 810

PAT甲级1155

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

weixin_43673669的博客 349

13、7自由度机械臂混合逆运动学算法解析

本文介绍了一种针对7自由度机械臂的混合逆运动学算法,通过引入额外参数Φ解决冗余自由度问题,并实现了避障、避免关节超限和扩大工作空间。该方法采用解析逆运动学求解策略,克服了机械臂在奇异位姿时的配置不连续性问题。通过对比实验验证了该算法在腕部关节角度连续性和位姿可达性方面的优越性,同时结合躯干逆运动学显著扩展了机械臂的工作空间。

rgv23456789的博客 236

【AUTOSAR-E2E】-2.3-配置E2E:基于E2EPW配置一个RX接收的Signal Group(使用E2E Profile 1)

《共13页精讲》:此法也适用于E2E Profile 4/5/6/7/11/22 目录 1 检查CAN Matrix Arxml/DBC是否满足E2E需求 2 在DaVinci Developer中为Port添加E2E保护 3 在DaVinci Configurator settings设置 4 如何使用应用层的E2E保护 5 结尾 <--返回「Autosar_BSW高阶配置」总目录--> <--返回「Autosar_BSW高阶配置」专栏主页--> <-.

「汽车电子助手」的博客 3950

PAT 甲级 1155 Heap Paths (30分)

1155 Heap Paths

Droke_Zhou的博客 361

手把手教你学pcie--性能调优 - 中断机制优化

在PCIe系统开发中,中断机制是主机与设备之间通信的重要组成部分。然而,不当的中断设计可能导致CPU负载过高、延迟增加等问题。本文将详细介绍如何优化PCIe系统的中断机制,提升系统性能和响应速度。传统中断(INTx):通过专用引脚发送中断信号,可能引发冲突且效率较低。消息中断(MSI):通过内存写操作发送中断消息,避免冲突并提高效率。扩展消息中断(MSI-X):基于MSI改进,支持更多独立中断向量,适用于复杂场景。选择合适的中断模式对系统性能至关重要。

MHD0815的博客 283

PAT 甲级 1155  Heap Paths

1155Heap Paths(30 point(s)) 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 eit...

漫浸天空的雨色的备忘录~ 853

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

【PAT A1155】Heap Paths (30 分)

1155Heap Paths(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 gr...

tqlwsl的博客 321

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

PAT 甲题 1155 Heap Paths

PAT 甲题 1155 Heap Paths思路:代码: 最近写程序有点迷,很简单的逻辑都写不清楚; 柳婼学姐推荐PAT从后往前刷,fine,先写最后一题,虽然30分,但是很简单,很短就AC了; 虽说程序短,逻辑简单,但是我还是调试了一段时间才AC,挫败感萌生。。。 思路: 深度优先搜索 代码: #include<iostream> #include<algorithm> ...

Yuhan的博客 279

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【PAT甲级题解记录】1155 Heap Paths (30 分)

问题描述 给一棵完全二叉树,求其是否为堆,要求输出大根堆还是小根堆,并且输出每一条自上而下的分支路径。 解题思路 对于路径的输出,利用dfs(其实就是二叉树的NRL遍历,和先序遍历差不多,先序遍历是NLR)配合动态维护vector就可以完成。 至于堆性质的判定,我们可以直接判断heap[0]和heap[1]的大小顺序,然后以此顺序为参照,在dfs里判断path是否按照该顺序排序。

retr0的博客 344

PAT Advanced 1155 Heap Paths (30 )

PAT Advanced 1155 Heap Paths (30 )题目描述Input Specification:Output Specification:Sample Input:Sample Output:解题思路Code 题目描述 In computer science, a heap is a specialized tree-based data structure that sati...

gq__97的博客 321

PAT甲级1155 Heap Paths (30分)|C++实现

一、题目描述 原题链接 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)

weixin_42393947的博客 707

PAT甲级A1155 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...

vir_lee的博客 343

PAT甲级1155 Heap Paths (30 分)

1155Heap Paths(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 gre...

Dexter 227

PAT甲级 1155 Heap Paths (30分) DFS+堆/C++

1155 Heap Paths (30分) 题目大意:给定一个完全二叉树,从右到左输出其所有到叶子结点的路径,并判断其是否为堆。 看到输出路径很自然的会想到DFS,在每一次往下搜索时,都将结点pop进path当中,直到叶子结点。 检查完左孩子再检查右孩子,当左右孩子都检查完,则pop出当前根 判断是否为堆可以看我的另外一篇题解~ 题解:1147 Heaps (30分)(下面代码中对于堆的判断较该题解有所改进) #include<iostream> //输入输出

qq_43955154的博客 215

PAT甲级 1155 Heap Paths (30 分)

题意 判断一个完全二叉树是不是堆 代码 #include <bits/stdc++.h> using namespace std; const int N = 1e3 + 10; typedef long long ll; struct node{ int data; node *left, *right; node(){} node(int a):data(a){left ...

darknight 347

甲级PAT 1155 Heap Paths (30 分)(dfs,堆排序)

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

Joyceyang_999的博客 255

PAT甲级 1155 Heap Paths (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 than or equa...

Jasonare的博客 294

PAT甲级 -- 1155 Heap Paths (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 than or equal to (i...

猪の成长日记 227
上一篇: PAT甲级-1154 Vertex Coloring (25 分) ( set + 枚举 / dfs )
下一篇: 随笔 -( 二叉树 )
Cosmic_Tree
博客等级 码龄7年 134粉丝 337原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值