5-1 Build A Binary Search Tree (30分)

PAT-甲级-1099 Build A Binary Search Tree (30 分) 1099Build A 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... 阅读详情

5-1 Build A 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.

Given the structure of a binary tree and a sequence of distinct integer keys, there is only one way to fill these keys into the tree so that the resulting tree satisfies the definition of a BST. You are supposed to output the level order traversal sequence of that tree. The sample is illustrated by Figure 1 and 2.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤\le≤100) which is the total number of nodes in the tree. The next N lines each contains the left and the right children of a node in the formatleft_index right_index, provided that the nodes are numbered from 0 to N-1, and 0 is always the root. If one child is missing, then -1 will represent the NULL child pointer. Finally N distinct integer keys are given in the last line.

Output Specification:

For each test case, print in one line the level order traversal sequence of that tree. All the numbers must be separated by a space, with no extra space at the end of the line.

Sample Input:

9
1 6
2 3
-1 -1
-1 4
5 -1
-1 -1
7 -1
-1 8
-1 -1
73 45 11 58 82 25 67 38 42

Sample Output:

58 25 82 11 38 67 45 73 42

#include<stdio.h>
#include<stdlib.h>
#include<math.h>

struct Tree{
	int num;
	int left;
	int right;
};

int main(void)
{
	int n, m, temp, x, top, k, number;
	scanf("%d",&n);
	struct Tree a[n], c[n];
	struct Tree T;
	int b[n], d[n];
	int front, rear;
	for(int i=0; i<n; i++)
	{
		scanf("%d %d",&m,&x);
		a[i].left=m;
		a[i].right=x;
	}
	for(int i=0; i<n; i++)
	{
		scanf("%d",&b[i]);
	}
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<n-1; j++)
		{
			if(b[j]>b[j+1])
			{
				temp=b[j];
				b[j]=b[j+1];
				b[j+1]=temp;
			}
		}
	}
	number=0;
	top=0;
	k=0;
	while(top!=0||number!=-1)
	{
	    while(number!=-1)
	    {
		    d[top++]=number;
		    number=a[number].left;
	    }
	    number=d[--top];
	    a[number].num=b[k++];
	    if(number!=-1)
	    {
	    	number=a[number].right;
		}
	}
	number=0;
	c[0]=a[0];
	front=0;
	rear=1;
	while(front!=rear)
	{
		number=c[front].left;
		if(number!=-1)
		{
			c[rear++]=a[number];
		    
	    }
		number=c[front++].right;
		if(number!=-1)
		{
		    c[rear++]=a[number];
	    }
	}
	for(int i=0; i<n; i++)
	{
		if(i!=n-1)
		{
			printf("%d ",c[i].num);
		}
		else
		{
			printf("%d",c[i].num);
		}
	}
}


Binary search tree C++ 课上组内共同完成,由导师监督。有一定参考价值 立即下载

相关推荐

binary search tree 二叉搜索树的C++实现,有插入、删除、查找、查找最大最小等功能

binary search tree 二叉搜索树的C++实现,有插入、删除、查找、查找最大最小等功能,并附有测试例子,简单易懂

7-20 Binary Search Tree

A binary search tree is uniquely determined by a given ordered insertions of a sequence of positive integers. On the other hand, a given binary search tree may correspond to several different insertion sequences. Now given several insertion sequences, it i

小L的博客 496

PySpark 本地开发环境搭建与实践

本文详细介绍了 PySpark 本地开发环境的搭建过程,包括 JDK、Hadoop、Anaconda、PySpark 的安装以及 Pycharm 工程的创建。同时,深入讲解了代码编写、本地开发案例(如 WordCount、处理特殊分隔符、读取 hdfs 数据、获取外部变量)、Spark 程序的监控和 local 模式下结果文件数量问题等内容。通过掌握这些知识和技能,读者可以在 Windows 本地环境中高效地进行 PySpark 开发,处理大规模数据,解决实际业务中的数据分析和处理问题。

天冬忘忧的博客 3012

C++二叉搜索树

C++二叉搜索树

weixin_63246738的博客 525

海上AI导航系统:技术架构与行业应用解析

计算机视觉与强化学习技术在海上导航系统中的应用正逐步改变传统航运业。通过多模态传感器融合技术,现代AI导航系统能够实现高精度的物体识别与实时路径规划,有效解决能见度依赖、决策延迟等核心痛点。以Orca AI的SeaPod系统为例,其结合了NVIDIA GeForce RTX 4070的边缘计算能力与多光谱协同识别技术,在恶劣环境下仍能保持稳定性能。这类系统不仅提升了航行安全性,还通过优化航线显著降低燃油消耗,在东京湾、马六甲海峡等繁忙水域展现出巨大价值。随着毫米波雷达融合方案的测试推进,未来海上导航将突破极

weixin_33597080的博客 354

PlatformIO串口监视器波特率配置实战:从乱码到清晰输出的关键步骤

本文针对PlatformIO串口监视器常见的乱码问题,深入解析了其根源在于波特率不匹配。文章核心在于指导开发者通过修改项目根目录下的platformio.ini配置文件,正确设置monitor_speed参数,确保与代码中Serial.begin()的波特率一致,从而实现从乱码到清晰输出的关键步骤。此外,还介绍了其他实用的串口监控配置与常见避坑指南。

weixin_29183637的博客 389

C++实现搜索二叉树(Binary Search Tree)

对只有一个key的二叉搜索树稍加改造,这样一个key,value模型就完成了,这种KV模型存在的意义便是,可以通过找到key所在的位置从而找到该位置的val,这个模型的运用场景有很多,以下有一段示例代码。另外还可以利用KV模型进行计数操作,记录key出现的次数,示例代码如下所示。以上便是关于搜素二叉树的实现和运用,希望能够班帮助到大家。

2301_81261654的博客 519

保姆级教程:用SAP PS管理电梯/大型设备项目(ETO/ATO模式)从成本收集到开票结算

本文详细解析了SAP PS模块在ETO/ATO项目中的全流程成本管理实践,特别针对电梯/大型设备制造行业。从成本收集到开票结算,通过WBS元素、网络活动等核心架构设计,结合F-02、CJ20N等事务代码,实现设计、生产、物流及现场服务各阶段的精准成本控制,助力企业提升项目利润率与财务效率。

weixin_30444105的博客 457

C++:二叉搜索树

此时我们需要多定义两个指针,一个指向被替换节点,一个指向被替换节点的父节点,此时被替换节点replace节点指向要删除节点的右节点,如果replace节点不为空,那么一直更新replace节点的位置,直到找到右子树中的最左节点,此时replace节点就是最左节点,将cur节点的值和replace节点的值交换,如果replace节点位于其父节点的左侧,将replaceparent节点的左节点指向replace节点的右节点,否者将replaceparent节点的右节点指向replace节点的右节点。

2302_80892233的博客 913

SAP SD模块进阶:VA31和VA01底层竟是同一个BAPI?揭秘销售单据创建的统一逻辑

本文深入解析SAP SD模块中VA31和VA01的底层统一架构设计,揭示两者如何通过同一个BAPI `SD_SALESDOCUMENT_CREATE`实现销售单据创建。通过ABAP调试和参数化设计分析,展示了SAP如何利用单据类型(doc_type)等关键字段实现业务逻辑复用,为开发者提供配置化设计的重要启示。

weixin_30561177的博客 244

Android音频学习(八)——AudioFlinger线程

在 PlaybackThread 的实现中,Flag 用于标识线程的特殊行为或配置。

miemie2014的博客 1399

二叉搜索树(Binary Search Tree)--C++

二叉搜索树又叫二叉排序树,是二叉树的一种特殊情况。树的每个节点的值都满足一定的规律,同样是以结构体为节点创造树。1.对于根节点有“左小右大”。2.对于左右子树节点有“左小右大”。呼~,总算是把二叉搜索树的主要内容讲完了,重点还在于二叉搜索树的操作那一部分,对于删除的情况分析以及递归的实现都能使我们更加了解二叉搜索树的整体结构。关于树形数据结构的知识现阶段我还在学习中,往后会继续更新相关知识的博客。👋httpshttpshttpshttpshttpshttpshttpshttps。...

qq_63412763的博客 747

2025年最新雷电模拟器9.22安装Magisk面具和LSPosed框架完整图文教程 文章末尾附所需软件下载

操作环境:雷电模拟器9.0.22 虚拟系统版本android 91、设置-其他设置-Root权限-开启2、设置-性能设置-System.vmdk可写入3、安装Magisk-18a0b94a-delta(25205).apk,运行并给予root权限4、选择右上角安装弹出“允许“magisk Delta”访问您设备上的照片、媒体内容和文件吗?“选择允许,然后结束magisk进程。5、从新运行magisk选择右上角安装-选择修补boot镜像中的vbmeta和安装到Recovery。

代码简单说 Vue、JAVA、PHP、Node.js 熟练运用,接口、架构、性能全搞定。 7094

PATA 1099 Build A Binary Search Tree(30 分)解题报告

1099 Build A 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 ...

热心市民小黎的博客 240

基于STM32F4XX 、HAL库的SHT40-CD1B-R3温湿度传感器驱动应用C语言程序设计

define SHT40_ADDR_READ 0x8B // CD1B 读地址 ((0x45 << 1) + 1)#define SHT40_ADDR_WRITE 0x8A // CD1B 写地址 (0x45 << 1)// 初始化 I2C1 (根据 CubeMX 生成的函数)// 湿度: -6%RH + 125%RH * (raw / 2^16)// 温度: -45°C + 175°C * (raw / 2^16)// 返回值: 0=成功, 1=失败(CRC错误或I2C通信错误)

PP_Cottage的博客 236

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

1099. Build A 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

焱然飞的知识收藏 280

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

1099 Build A Binary Search Tree (30分)

1.刚开始想的是构造树的时候,顺便存储每层的值,但是 有两个例子段错误 2.之后就只好按构造的树来进行层次遍历 3.题目理解错了,以为没告诉你两个节点的父节点,所以是一直递归,直到不能递归来构造树。 #include<iostream> #include<cstdio> #include<cstring> #include<queue...

❂ 286

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

Build A Binary Search Tree

5-1Build A Binary Search Tree(13分) 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...

kaggle expert,全球排名前1000,清华计算机研究生,兴趣算法工程 822

1099 Build A Binary Search Tree (30 分)

1099 Build A 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 w...

weixin_30667301的博客 194

1099 Build A 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...

weixin_42582136的博客 247

1099 Build A Binary Search Tree (30)

【代码】1099 Build A Binary Search Tree (30)

2401_88085478的博客 967
下一篇: 树的深度
csucbMIT
博客等级 码龄10年 7粉丝 15原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值