04-树6 Complete Binary Search Tree(30 分)

信奥赛csp初赛高频考点【位运算】(详细解析) 阅读详情

04-树6 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 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)N (≤1000)N(≤1000). Then NNN 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

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define MAXN 1000

//按顺序输出结果(层次遍历)
void Output(int T[], int n) {
	for (int i = 0; i < n; i++) {
		if (i == 0) printf("%d", T[i]);
		else printf(" %d", T[i]);
	}
	printf("\n");
}

//获取一个含有n个结点的完全二叉树的层数
int GetLevel(int n) {
	int count = 0;
	while (n) {
		count++;
		n /= 2;
	}
	return count;
}

//获取一个n层的满二叉树的总结点数
int GetSum(int level) {
	if (level < 1) return 0;
	int sum = 1;
	for (int i = 0; i < level; i++) sum *= 2;
	return --sum;
}

//获取一个满二叉树第n层结点的个数
int GetLevelSum(int level) {
	if (level < 1) return 0;
	int sum = 1;
	for (int i = 0; i < level - 1; i++) sum *= 2;
	return sum;
}

//获取一个含有n个结点的完全二叉树的左子树的节点数
int GetLeftLength(int n) {
	int Level = GetLevel(n), sum = 0;
	sum += GetSum(Level - 2);
	sum += (n - GetSum(Level - 1) < GetLevelSum(Level) / 2) ? n - GetSum(Level - 1) : GetLevelSum(Level) / 2;
	return sum;
}

//递归求解
void solve(int input[], int tree[], int inputLeft, int inputRight, int treeRoot) {
	int n = inputRight - inputLeft + 1, L, leftTreeRoot, rightTreeRoot;
	if (n == 0) return;
	L = GetLeftLength(n);
	tree[treeRoot] = input[inputLeft + L];
	leftTreeRoot = treeRoot * 2 + 1;
	rightTreeRoot = leftTreeRoot + 1;
	solve(input, tree, inputLeft, inputLeft + L - 1, leftTreeRoot);
	solve(input, tree, inputLeft + L + 1, inputRight, rightTreeRoot);
}

//比较函数,qsort函数会用到
int compare(const void* a, const void* b) {
	return *(int*)a - *(int*)b;
}

int main(void) {
	int n, input[MAXN], tree[MAXN];
	scanf("%d", &n);
	for (int i = 0; i < n; i++) {
		scanf("%d", &input[i]);
	}
	//对输入序列按从大到小的顺序排序
	qsort(input, n, sizeof(int), compare);
	//递归求解
	solve(input, tree, 0, n - 1, 0);
	Output(tree, n);
	return 0;
}
【VTK】VTK中的光标样式 本文分享VTK中的光标设置相关内容技术,希望对各位小伙伴有所帮助! 阅读详情

相关推荐

(三)从零开始学习模糊控制——模糊决策

模糊控制之基础——模糊数学 (一)模糊数学——基本概念 (二)模糊数学——模糊关系 (三)模糊数学——模糊决策 文章目录模糊控制之基础——模糊数学前言一、模糊决策法1.最大值原理2.模糊排序2.模糊寻优总结 前言   模糊决策是指在模糊环境下进行决策的数学理论和方法。这类问题─般具有大系统特征,系统之间的关系十分复杂,存在不能准确赋值的变量,这些变量属于模糊因素,涉及到一定的主观因素,使得子系统之间、变量之间的关系不清晰,从而必须借助排序、模糊评判等方法来进行处理。 一、模糊决策

qq_43667552的博客 4750

04-树6 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. ...

mumei的博客 1030

永磁同步电机定子槽型设计实战:从梨形槽到矩形槽的NVH优化之路

本文深入探讨了永磁同步电机定子槽型设计在NVH优化中的关键作用,对比了梨形槽与矩形槽的性能差异。通过Ansys Maxwell仿真和实际案例,揭示了矩形槽在提升槽满率、降低电磁噪声方面的优势,并总结了五大设计黄金法则,为电机设计工程师提供了实用指导。

weixin_28740299的博客 493

04-树6 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...

adai 966

华大HC32F460 TCP Client 实验

华大HC32F460 TCP Client 实验

ZhuoWuKeJi的博客 647

YOLOv11 改进 - SPPF模块 | 发论文神器!LSKA注意力改进SPPF,增强多尺度特征提取能力,高效涨点!!!

本文介绍了Large Separable Kernel Attention (LSKA)模块及其在YOLOv11中的结合应用。LSKA旨在解决Visual Attention Networks (VAN)使用大内核卷积时的计算效率问题,通过将2D深度卷积层的卷积核分解为级联的水平和垂直1-D卷积核,降低了计算复杂度和内存占用,且使VAN更关注物体形状。我们将集成LSKA的SPPF_LSKA模块引入YOLOv11,大量实验表明,改进后的模型在对象识别、检测、分割和稳健性测试上表现出色。

魔改工程师的博客 1279

04-树6 Complete Binary Search Tree (30 分)

#include&lt;cstdio&gt; #include&lt;stdlib.h&gt; const int maxn=1001; int arr[maxn], brr[maxn]; int pos=0, n; int cmp(const void *a, const void *b){ return(*(int *)a-*(int *)b); } void solve(int ro...

学习为主,偶尔扯淡 581

华为OD机试E卷 -猜字谜(Java & Python& JS & C++ & C )

小王设计了一个简单的猜字谜游戏,游戏的谜面是一个错误的单词,比如nesw,玩家需要猜出谜底库中正确的单词。猜中的要求如下:请你写一个程序帮忙在谜底库中找到正确的谜底。谜面是多个单词,都需要找到对应的谜底,如果找不到的话,返回”not found”

算法大师! 3487

PATA 1064 Complete Binary Search Tree(30 分)解题报告

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

热心市民小黎的博客 669

ANPC三电平拓扑双脉冲试验的原理与实施

ANPC三电平拓扑因损耗低、谐波小、应力均衡而广泛应用,但其存在短、长两类换流通道,寄生电感和开关应力差异显著。短通道尖峰小、可靠性高;长通道应力严苛。因此需分别制定两套独立双脉冲测试方案,针对不同时段和器件(如T1、T5、T6、T4及对应二极管)全面考核开关特性。

嵌入式老牛的博客 348

【推荐100个unity插件】unity易于使用模块化设计的天空、体积云和天气系统——Enviro 3

Enviro 3是一款功能强大的Unity天空与天气系统插件,提供动态天空、体积云、天气模拟等丰富功能。安装时需将预制体拖入场景并配置主相机,URP支持需激活深度纹理和添加渲染特性。模块化设计允许自由添加/移除时间、光照、雾效等模块,并通过API控制参数。推荐配合色调映射(如ACES模式)获得最佳视觉效果。插件支持季节、温湿度模拟及闪电风暴等特效,适用于需要逼真环境渲染的Unity项目。

向宇的博客,专注php/web全栈 unity游戏开发,欢迎大家评论纠错 2093

04-树6 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

时间杀手Time的博客 1528

数字电路与逻辑设计实验二 组合逻辑电路

1.组合逻辑电路功能测试 (1)用 2 片 74LS00 组成图 2.1 所示逻辑电路。为便于接线和检查,在图中要注明 芯片编号及各引脚对应的编号。 (2)图中 A、B、C 接逻辑开关,Y1,Y2 接发光管电平显示。 (3)按表 2.3 要求,改变 A、B、C 输入的状态,填表并写出 Y1,Y2 的逻辑表达式。 (4)将运算结果与实验结果进行比较。 2.测试用异或门(74LS86)和与非门组成的半加器的逻辑功能。 根据半加器的逻辑表达式可知,半加 器 Y 是 A、B 的异或,而进位 Z 是 A

欢迎交流学习心得 1277

04-树6 Complete Binary Search Tree (30分)

04-树6 Complete Binary Search Tree (30分) 原帖地址:Cnblogs 解题思路:这里要用到完全二叉树的性质,即如果有结点数为n的完全二叉树,对任一节点 i (1<=i<=n)都有,如果i=1,则i是二叉树的根,如果i>1,则其双亲是结点[i/2];如果2i>n,则结点i无左孩子,否则其左孩子是结点2i;如果2i+1>n,则结点i无右孩子,否则其右孩子是结点2i+1 从给出的样本可以知道,将输入的有序节点进行排序以后,得到了该完全二叉搜索树

m0_43429389的博客 359

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

04-树6 Complete Binary Search Tree (30 分)

1. 这道题除了陈越姥姥上课讲的思路之外,还有另一种思路:完全二叉搜索树的中序遍历是顺序序列,已知当前的顺序序列,可以倒推出原来的完全二叉搜索树。 2. 下面的代码填写的是姥姥讲的思路,通过公式计算他们的左儿子、右儿子的个数。 3. ![计算左子树的规模](https://img-blog.csdnimg.cn/20190430170646259.png?x-oss-process=image/w...

月亮下的小美丽???? 611

lwip-2.0.3

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

PTA习题:04-树6 Complete Binary Search Tree (30分)

04-树6 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...

wulila的博客 408

SIMATIC S7-1200, S7-1500 PID 控制[手册]

SIMATIC S7-1200, S7-1500 PID 控制[手册]

PTA 04-树6 Complete Binary Search Tree (30 分)

文章目录前言一、原题二、方法1.常规构建二叉搜索树2.直接利用数组总结 前言 学习数据结构,记录下过程 一、原题 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 s

qq_53006294的博客 650

04-树6 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 subt...

JJJ's blog 195
上一篇: 04-树5 Root of AVL Tree(25 分)
下一篇: JavaScript 实现全排列函数
centralunit
博客等级 码龄10年 219粉丝 115原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值