PAT - 1064 Complete Binary Search Tree (30 分) 完全二叉树和二叉搜索树

SAP 小结 关于SAP 开发的实习就要告一段落了,从刚开始接触,到现在已经6个月了,想要很简要的把自己对SAP的理解写出来,便于以后查阅(不定期更新)。 1. 什么是SAP? 广义的SAP,可以指代SAP公司以及SAP的各种产品,它的意义要在特定语境下才能确定。一般我们所说的SAP特指SAP ERP。 SAP ERP(enterprise resource planning)是一个企业信息系统。通过这个系 阅读详情

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:

  • The left subtree of a node contains only nodes with keys less than the node's key.
  • The right subtree of a node contains only nodes with keys greater than or equal to the node's key.
  • Both the left and right subtrees must also be binary search trees.

A Complete Binary Tree (CBT) is a tree that is completely filled, with the possible exception of the bottom level, which is filled from left to right.

Now given a sequence of distinct non-negative integer keys, a unique BST can be constructed if it is required that the tree must also be a CBT. You are supposed to output the level order traversal sequence of this BST.

Input Specification:

Each input file contains one test case. For each case, the first line contains a positive integer N (≤1000). Then N distinct non-negative integer keys are given in the next line. All the numbers in a line are separated by a space and are no greater than 2000.

Output Specification:

For each test case, print in one line the level order traversal sequence of the corresponding complete binary search tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line.

Sample Input:

10
1 2 3 4 5 6 7 8 9 0

Sample Output:

6 3 8 1 5 7 9 0 2 4

这道题自己百思不得其解,甚至想用数学规律的方法去解。

答案的思路让人一目了然: BST,中序遍历一定是递增数列;CBT,可以直接用数组存储。这样的话,直接在中序遍历的过程对树进行填充即可!

//对于BST,中序遍历序列一定是递增的; 那么首先sort,就可以得到中序遍历序列
//对于完全二叉树来说,可以用数组存储
//因此,可以对存储完全二叉树的数组进行中序遍历,在中序遍历的过程中complete the tree
// AMAZING!!!
//题,总是有方法的,联想自己学过的知识点
#include<cstdio>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;

const int maxn = 1050;
int tree[maxn]; // 从1到n存储cbt
vector<int> origin;

int n;
int index = 0;

void InOrder(int root) {
	if (root > n)
		return;
	InOrder(2 * root);
	tree[root] = origin[index++];
	InOrder(2 * root + 1);
}

int num = 0; //已输出的元素个数

void LevelOrder(int root) {
	queue<int> q;
	q.push(root); // push the index
	while (!q.empty()) {
		int root = q.front();
		printf("%d", tree[root]);
		num++;
		if (num != n) printf(" ");
		q.pop();
		if (2 * root <= n)
			q.push(2 * root);
		if (2 * root + 1 <= n)
			q.push(2 * root + 1);
	}
}

int main() {
	int tmp;
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &tmp);
		origin.push_back(tmp);
	}
	sort(origin.begin(), origin.end());
	InOrder(1);
	LevelOrder(1);

	return 0;
}

 

基于混合智能框架的景区边界范围预测与补全系统 摘要 本文提出一种混合智能框架,结合随机森林模型和GIS规则约束,解决景区边界缺失问题。系统基于Python和ArcGIS平台,利用多源地理空间数据训练随机森林分类器预测景区范围,并集成地形、水文、道路等GIS规则进行优化。实验结果表明,该方法能有效预测15000个景区的边界,精度优于单一模型。创新点在于: 1)数据驱动与知识驱动相结合的混合智能范式; 2)面向全国尺度的大规模边界补全; 3)提供完整的Python+ArcGIS实现方案。该系统为景区精细化管理提供了技术支撑,具有实用价值。 阅读详情

相关推荐

Temporal Fusion Transformer(TFT)与扩散模型融合:时间序列预测新范式

本文探讨了Temporal Fusion Transformer(TFT)与扩散模型融合的时间序列预测新范式。该融合方案利用TFT强大的特征提取与可解释性,为扩散模型提供精准的“导航图”,从而在噪声数据中实现鲁棒预测,并原生支持不确定性量化,输出完整的概率分布与置信区间,为金融、能源等领域的风险决策提供更可靠的依据。

weixin_29096377的博客 292

LeetCode二叉树构造方法,通过一维数组直接构建完整的二叉树(按照LeetCode的格式)

1.LeetCode中二叉树的结构 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ 以上是Leetcode中二叉树的结构,然而在题目通常会这样给出 那.

USTC暖暖的博客 3341

Python 中调用 R 语言的教程(包含编码问题解决办法)

rpy2 是一个 Python 库,允许在 Python 中直接运行 R 代码并实现数据转换。安装需确保 Python 和 R 环境正确配置,Windows 用户需注意编码问题(GBK 错误可通过控制面板调整区域设置解决)。基本用法包括执行 R 代码、传递数据(支持 Python 与 R 数据类型自动转换)以及调用 R 包函数。通过 robjects.r() 执行 R 代码,使用 pandas2ri 转换器处理 DataFrame 数据交换,并支持像导入 Python 模块一样加载 R 包(如 import

zygalaxy的博客 997

PAT A1064 Complete Binary Search Tree (30 分) 完全二叉树 二叉搜索树

题目大意:给出一个序列,构造是完全二叉树的BST,输出层序遍历结果。 这一题要同时结合完全二叉树和BST的性质来做。完全二叉树可以用一个数组 bst 存储,而BST的中序序列有序。题目已经说了是N个不同的整数,因此也不用考虑重复元素到底放左子树还是右子树了。对中序遍历的函数进行改写,在中序遍历到某节点时,按顺序将中序序列的元素赋给遍历到的节点。这样就完成了bst的赋值,输出数组 ...

HackingGate的博客 171

UR机械臂正逆运动学解析:从DH参数到8组解的完整求解策略

本文深入解析UR机械臂的正逆运动学求解策略,从DH参数原理到完整的8组解求解方法。详细介绍了正运动学的坐标变换过程,以及逆运动学中Atan2函数和三角恒等变换的应用,并揭示了8组解的产生机制。通过实际案例和调试技巧,帮助工程师掌握UR机械臂运动学求解的核心技术。

weixin_29094775的博客 414

手把手教你用Python+Playwright实现抖音/B站/视频号三平台自动上传(附完整配置流程)

本文详细介绍了如何使用Python和Playwright构建一个多平台视频自动化上传系统,涵盖抖音、B站和视频号三大平台。文章从环境搭建、身份验证持久化、平台特异性上传引擎设计到任务调度与错误处理,提供了完整的配置流程和实战代码,帮助开发者实现高效、稳定的批量上传功能。

weixin_29294975的博客 1155

PAT (Advanced Level) Practice A1064 Complete Binary Search Tree (30 分)(C++)(甲级)(完全二叉树、二叉搜索树)

原题链接:A1064 Complete Binary Search Tree #include&lt;algorithm&gt; #include&lt;iostream&gt; #include&lt;cstdio&gt; using namespace std; int S[1010], T[1010], N, i = 1, j = 1; void inOrder(int t)//按中序遍...

izhxxx的博客 306

基于MATLAB开发AUTOSAR软件应用层模块-part2.建立一个AUTOSAR的模型,并生成autosar格式代码

上一篇我们建立了一个空模型,这篇我们接着上篇文章,建立一个简单的模型 点击 Library Browser,打开simulink模块库,见下图 左侧是模块库的列表,右侧是每个模块库里包含的具体的模块。 点击通用模块库,里边是一些常见的模块, 我们选取里边的3个模块,分别是输入,输出和增益三个模块,右键点击模块,选择add block to model xx,可以把模块添加到模型中。 然后练好连线 一个简单的模型就搭建好了,之后我们修改配置,将模型修改为AUT...

汽车软件开发 3303

1064 Complete Binary Search Tree——PAT甲级

大致思路是,先建立完全二叉树的框架,其中的数字是什么无所谓,之后将题目给出的数据经过排序后按中序遍历的方式插入完全二叉树。

flyidj的博客 2090

ABAP Platform 2025 到来之后,我们该怎样重新理解 SAP S/4HANA 2025 的开发边界

2025年10月,SAP正式发布S/4HANA Cloud Private Edition 2025及S/4HANA 2025,其技术底座ABAP Platform 2025全面支持SAP HANA数据库。新版平台强调Clean Core理念升级为四级成熟度模型,推动ABAP Cloud成为扩展开发首选方案。AI能力正式融入ABAP开发全流程,包括代码生成、迁移辅助等场景。RAP框架显著增强,支持跨业务对象协作、树形视图等复杂业务场景。ADT开发工具持续进化,新增内存分析、单元测试等专业功能,形成完整开发工

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 315

手把手教你学Simulink--基于未来出行解决方案的场景实例:电动飞行汽车(eVTOL)的动力系统建模与仿真

电动垂直起降飞行器零道路拥堵:三维空间飞行绿色低碳:全电驱动,零排放快速响应:城市内15分钟直达低噪音:相比直升机↓20dB📊 预测:2030年全球eVTOL市场规模将超300亿美元,中国、美国、欧洲加速适航认证。本文通过,构建了电动飞行汽车(eVTOL)动力系统高压电池系统建模电力电子(DC-DC、逆变器)实现PMSM电机+螺旋桨推进系统搭建完整飞行任务动态仿真性能KPI评估与优化。

MHD0815的博客 444

浙大PAT 1064. Complete Binary Search Tree

1064. Complete Binary Search Tree (30) 时间限制 100 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue A Binary Search Tree (BST) is

凌风 6417

CEEMDAN算法及其应用

本博文主要介绍CEEMDAN算法的原理及其在信号去噪方面的应用

总结论文中的算法并给出论文的复现代码 6万+

PAT 甲级 1064  Complete Binary Search Tree

1064 Complete Binary Search Tree (30 point(s)) A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes w...

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

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

PAT甲级 1064 Complete Binary Search Tree (30 分)

\quad不用尝试着建树,注意完全二叉树如果在数组中按照一层一层标号,这样可以得到一个规律:孩子节点的下标为i则其左孩子节点的下标为2i,右孩子节点的下标为2i+1。因此我们可以尝试着根据这个规律建树,我们直到BST的中序遍历是有序的,因此我们先把输入的数从很小到大排序,利用中序遍历左根右的思想用tree[n]来模拟建树过程,这样存放在tree[n]中的数即为层序遍历答案。程序如下: #inclu...

qq_40438165的博客 1772

visio安装包

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

1064 Complete Binary Search Tree

题目来源:PAT (Advanced Level) Practice A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node cont

weixin_51032998的博客 165

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

PAT(Advanced)1064 Complete Binary Search Tree C++实现

PAT(Advanced)1064 Complete Binary Search Tree C++实现 题目链接 1064 Complete Binary Search Tree 题目大意 给定没有相同元素的非负整数序列,构造其完全二叉排序树并输出层次遍历序列,完全二叉排序树即为同时满足二叉排序树和完全二叉树要求的二叉树 算法思路 中序遍历构造完全二叉搜索树,保证叶子节点集中在最后一层左边,满足完全二叉树定义,根据二叉搜索树定义,中序遍历序列递增有序。每次递归到底选取优先级队列关键字最大的元素,先遍历右子树

qq_44486439的博客 454

【PAT甲级 - C++题解】1064 Complete Binary Search Tree

📣专栏定位:为想考甲级PAT的小伙伴整理常考算法题解,祝大家都能取得满分!

Newin2020的博客 722

[PAT]1064 Complete Binary Search Tree (30 分)

一、题意 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The ri...

刘好念的博客 242

PAT甲级.1064. Complete Binary Search Tree (30)

题目 A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The righ

R11happy的博客 499

1064 Complete Binary Search Tree(30分)-PAT甲级

A Binary Search Tree (BST) is recursively(递归地) defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes with keys great

神笔码农.的博客 289

【PAT(甲级)】1064 Complete Binary Search Tree

既然已经可以得到二叉搜索树的中序输出,那么我们可以用数组来模拟树,中序输出的顺序是左——根——右;假设根节点下标为i,左子树就是2*i,右子树就是2*i+1;由此递归出的数组,从前往后刚好就是二叉搜索树的层序输出。由题意,这棵二叉搜索树必定存在,且节点就是给出的完全二叉树的节点,那么二叉搜索树的中序输出是从小到大规则排序的,那我们是不是把完全二叉树的节点由小到大排序,就可以得到二叉搜索树的中序输出了?题目给出一个完全二叉树的节点信息,让你根据这颗完全二叉树建立唯一存在的一棵二叉搜索树,并层序输出其节点。

weixin_55202895的博客 313

1064. Complete Binary Search Tree (30)-PAT

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The righ

兵临城下 1591

PAT--1064 Complete Binary Search Tree (30 分)

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right su...

jackson_j的博客 149

【PAT】A1064 Complete Binary Search Tree【树】

A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The right subt...

elegant coder 794

PAT 1064 Complete Binary Search Tree (30 分)

题目: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node’s key. The righ...

xiao1guaishou的博客 170
上一篇: PAT甲级 - 1094 The Largest Generation (25 分) 树+DFS
下一篇: PAT甲级 - 1099 Build A Binary Search Tree (30 分) 重建二叉搜索树
DedicateToAI
博客等级 码龄9年 6粉丝 90原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值