PAT 1167 Cartesian Tree

19年冬季第四题 PAT甲级 1167 Cartesian Tree (30分)超级详解不AC都难 这道题的题眼: 1.给出的是个min-heap 最小堆,即所有父节点不大于子节点的二叉树。如果找到了目前区间的最小值,那就是个父节点。 2.给出的顺序是inorder 中序,也就是LNR。是不是想到了递归和分而治之的老方法了? 3.输出的顺序是level-order 层序。如果不建树,就是开个大数组,按照下标放;如果建树,就得用BFS遍历树了。 这道题的陷阱: 没有说是哪种二叉树,如果是最极端的那... 阅读详情

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.

CTree.jpg

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 (≤30), 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

代码长度限制

16 KB

时间限制

400 ms

内存限制

64 MB

按照题目要求构建二叉树,再层次遍历即可

#include <iostream>
#include <vector>
#include <queue>
using namespace std;
const int N = 31;
struct node{
	int ele=-1;
	int lchild=-1;
	int rchild=-1;
} nodes[N];
int min_heap=-1;
int n;
int gol_root;
vector<int> level_order;
void search(int fa, int l, int r)
{
	if(l>r) return;
	int root=l;
	for(int i=l+1 ; i<=r ; i++)
		if(nodes[i].ele<nodes[root].ele)
			root = i;
	if(fa == -1) gol_root = root;
	else if(nodes[fa].lchild == -1) nodes[fa].lchild=root;
	else nodes[fa].rchild=root;

	search(root, l, root-1);
	search(root, root+1, r);
}

void level_tra(int root)
{
	queue<int> q;
	q.push(root);

	while(!q.empty())
	{
		int x = q.front();
		level_order.push_back(nodes[x].ele);
		q.pop();
		if(nodes[x].lchild!=-1) q.push(nodes[x].lchild);
		if(nodes[x].rchild!=-1) q.push(nodes[x].rchild);
	}
}

int main()
{
	cin>>n;
	for(int i=1 ; i<=n ; i++)
	{
		cin>>nodes[i].ele;
	}
	search(-1,1,n);
	level_tra(gol_root);
	for(int i=0 ; i<level_order.size()-1 ; i++)
		cout<<level_order[i]<<" ";
	cout<<level_order[level_order.size()-1];
	return 0;
}


74HC165级联踩坑实录:STM32读取32路开关状态,时序调试与常见问题排查 本文详细介绍了74HC165级联在STM32读取32路开关状态时的时序调试与常见问题排查。从硬件设计的PCB布局优化、电源噪声控制,到软件时序的精确调校和数据采集策略,提供了完整的解决方案。通过逻辑分析仪和示波器的高效使用,解决了信号延迟和干扰问题,最终实现了稳定可靠的32路信号读取。 阅读详情

相关推荐

手把手教你用89C52单片机驱动0.96寸OLED(IIC接口,附完整代码)

本文详细介绍了如何使用89C52单片机通过I2C接口驱动0.96寸OLED显示屏,包括硬件连接、I2C通信原理、SSD1306驱动芯片的初始化及显存管理技巧,并提供了完整的代码示例和常见问题排查方法,帮助电子爱好者快速掌握OLED显示技术。

weixin_30781433的博客 370

PAT 1167 Cartesian Tree(30)

PAT 1167 Cartesian Tree 含注释

ljhsq的博客 332

Android Settings 设置Preference置灰显示

使Preference置灰显示,不能点击。先调用Preference的setEnabled(false),再调用setShouldDisableView(true); 如果使置灰项恢复正常可点击,可调用setEnabled(true)。

随风而动 1048

PAT 1167 Cartesian Tree 代码顶好理解的解法

【代码】PAT 1167 Cartesian Tree 代码顶好理解的解法。

weixin_68729092的博客 294

[Pytorch案例实践001]手写MNIST数字识别

Pytorch实现手写数字识别,带完整数据集。

2403_83044722的博客 1115

用Cursor+Node.js零代码搭建健康数据仪表盘:从数据录入到可视化全流程

本文详细介绍了如何利用Cursor AI编程助手和Node.js,无需手动编写代码,即可构建个人健康数据仪表盘的全流程。通过自然语言指令,实现从数据录入、存储到动态图表可视化的完整系统,展示了AI驱动开发在降低技术门槛、提升开发效率方面的强大能力。

gitlab7runner的博客 700

PAT 1167 Cartesian Tree(30分)

PAT 1167 Cartesian Tree 关键词:建树、BFS 题目大意: 给出二叉最小堆的中序遍历序列,输出层序遍历结果。 整理时间:2020.7.23

cwtnice的博客 711

【Nordic实战】nRF54L15引脚规划:电源域、专用引脚与低功耗设计避坑指南

本文深入解析Nordic nRF54L15芯片的引脚规划策略,重点阐述其基于电源域(MCU域、PERI域、LP域)的GPIO分配原则。文章提供了从思维转变到实战避坑的完整指南,强调遵循专用引脚与域对应关系是实现超低功耗设计的关键,并分享了系统化的引脚规划流程与工具使用建议。

weixin_29192141的博客 321

PAT甲级 1167 Cartesian Tree

浙江大学PAT题目解答内容. Contribute to ZouJiu1/PAT development by creating an account on GitHub.GitHub - ZouJiu1/PAT: 浙江大学PAT题目解答内容。

邹九的个人博客 227

车载 ECU 中的 Safety Linux 详解

车载 ECU 中的 Safety Linux 详解

天赐好车的博客 312

Altium Designer(AD24/25)如何在PCB设计阶段做阻抗匹配

目前阻抗匹配多直接有制版厂进行,PCB设计师在设计阶段可不关注阻抗设计,在加工阶段只需标注需要控制的阻抗线,制板厂即可按照要求对阻抗线进行调整和控制。但是对于设计师来讲掌握阻抗线的设计方法也是非常必要的。本文介绍Altium Designer(AD24/25)如何在PCB设计阶段做阻抗匹配。

fydar的博客 893

1167 Cartesian Tree – PAT甲级真题

ACartesian treeis 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 Cartesia..

柳婼 の blog 2553

使用MATLAB的COMSOL LiveLink:实时连接和数据传输

COMSOL LiveLink是一种功能强大的MATLAB工具包,它允许用户在MATLAB环境中与COMSOL Multiphysics软件进行实时连接和数据传输。COMSOL LiveLink是一种功能强大的MATLAB工具包,它允许用户在MATLAB环境中与COMSOL Multiphysics软件进行实时连接和数据传输。使用COMSOL LiveLink,你可以进行更复杂的模型设置和数据传输使用MATLAB的COMSOL LiveLink:实时连接和数据传输。

PixelNinja的博客 2093

1167 Cartesian Tree (PAT甲级)

这道题柳婼的解法太简洁了,把层序遍历这一步直接省略了,很厉害。index << 1 | 1 这个写法也很有趣,学习学习。

linh2006的博客 188

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

1167 Cartesian Tree (30 分) PAT

ACartesian treeis 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 Cartesia..

zhang12308的博客 1412

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

PAT甲级 1167 Cartesian Tree(30)

思路:碰到层序遍历就想到队列,也就是在区间中,先找到最小值(根结点)下标,入队。然后分别找左右子树中的根节点入队,以此类推。另外需要维护一个visited数组,作为遍历找根节点时的结束位置。

Hackeryuan的博客 294

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

19年冬季第四题 PAT甲级 1167 Cartesian Tree (30分)

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

白小飞 305

【PAT】PAT_1167_Cartesian_Tree

PAT备考刷题

hongshaoliyu的博客 131

1167 Cartesian Tree (30 point(s)) PAT甲级

1167 Cartesian Tree (30 point(s)) 题目 A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of

neko的博客 281

PAT (Advanced Level) Practice 1167 Cartesian Tree

题目 堆是一棵完全二叉树,树中每个结点的值都不小于(或不大于)其左右孩子结点的值。 这是一道简单题。但是,本菜鸡对于堆的定义不了解。所以,卡住了。而且,这应该是本菜鸡第一次用指针的写法,完成的一道题目。 #include <bits/stdc++.h> using namespace std; const int N=33; const int INF=0x3f3f3f3f; int num[N], res[N]; struct node{ int data; node* l

题解的个人总结,仅供参考 1182

2019年12月PAT甲级 第四题 Cartesian Tree(1167)题解

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

时间的玫瑰的博客 680

PAT甲级——1167 Cartesian Tree (30 分)(buildTree + 层次遍历)

思路: 读入in序列 然后根据in序列,buildTree,逻辑就是找到min的index,左边就是左子树,右边就是右子树(跟知道p + i)差不多 最后queue遍历 src: #include<bits/stdc++.h> using namespace std; // Cartesaion Tree 最小就是根,找到最小左右就是左右子树 struct node { int val; node* left; node* right; }; // 存一下inor..

小楼一夜听春雨 484

PAT (Advanced Level) - 1167 Cartesian Tree

DFS建树,离散化,BFS

https://www.hyperplasma.top 263

PAT (Advanced Level) Practice 1167 Cartesian Tree (30 分)

1167 Cartesian Tree (30 分) 题意:给一组数,求生成的笛卡尔树的层级遍历 笛卡尔树(Cartesian tree): 每次选取当前区间[l,r]中val的最小值所在的pos作为子树的根节点 然后对于[l,i−1],[i+1,r]递归地不断重复上述过程 写的递归,访问到每一个点的时候递归了几层记录下来,(也算一个标志区分不同的区间),根据这个层数输出结果(数据量小无所畏惧直接n^2) #include<bits/stdc++.h> using namespace std;

qq_40468936的博客 271

A1167 Cartesian Tree(30分)PAT甲级 (Advanced Level) Practice(C++)满分题解【最小堆+构建树+层序遍历】

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 Cartesia

qq_47677800的博客 190
上一篇: C++类构造函数与带参数的new
下一篇: PAT 1120 Friend Numbers
CocaCona
博客等级 码龄5年 21粉丝 11原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值