1099 Build A Binary Search Tree (30 point(s))

动态知识图谱构建:从本体论到工程实践 知识图谱作为语义网和人工智能领域的核心技术,旨在通过结构化的方式表示和组织知识。其核心原理在于将实体、概念及其关系建模为图结构,从而支持复杂的语义推理和关联查询。这一技术为机器理解数据间的深层联系提供了基础,在智能推荐、企业知识管理和学术研究等领域具有重要价值。通过结合自然语言处理(NLP)和图数据库技术,系统能够从非结构化数据中自动抽取知识,并构建动态演化的认知网络。本文以“本体论章鱼”项目为例,探讨了如何实现从静态本体到动态知识网络的范式转变,并详细解析了其分布式感知、适应性学习的架构设计,为构建灵活可 阅读详情

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

figBST.jpg

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (≤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 format left_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

考察点:

1. 知识点:BST的中序遍历是非递减序列;

2. 静态数组建立二叉树;

3. 利用递归完成树的中序遍历;

4. 利用队列完成树的层次遍历。

思路:

根据题目的图片实例和输入方式,此题适当使用结点静态数组保存二叉树。由于这是一棵二叉搜索树,因此只需要将给出的序列排序后,就可以得到它的中序遍历。因此将树的结构建立起来以后,对整棵树中序遍历(左子树--根--右子树),将按照元素大小排序后的序列依次插入二叉搜索树中。

最后借助队列,输出这棵二叉搜索树的层次遍历。

我的代码:

#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
struct Node{
	int data;int left,right;
	Node(){	data = 0;left=right=-1;	}
};
Node node[107];
int a[107];
int idx = 0;
void insert(int root){//中序遍历,同时完成赋值 
	if(node[root].left!=-1){
		insert(node[root].left);
	}
	node[root].data = a[idx];idx++;
	if(node[root].right!=-1){
		insert(node[root].right);
	} 
}
vector<Node> levelOrder(){//利用队列完成层次遍历 
	queue<Node> q;
	vector<Node> ans;
	q.push(node[0]);
	while(!q.empty()){
		Node cur = q.front();
		ans.push_back(cur);
		q.pop();
		if(cur.left!=-1) q.push(node[cur.left]);
		if(cur.right!=-1) q.push(node[cur.right]);
	}
	return ans;
}
int main(void){
	int N;cin>>N;
	for(int i=0;i<N;i++){
		cin>>node[i].left>>node[i].right;
	}
	for(int i=0;i<N;i++) cin>>a[i];
	sort(a,a+N); 
	insert(0);
	vector<Node> order = levelOrder();
	for(int i=0;i<order.size();i++){
		if(i==0) cout<<order[i].data;
		else cout<<" "<<order[i].data;
	}
	return 0;
}

 大神更简洁的代码(思路一致):

参考链接:https://blog.youkuaiyun.com/richenyunqi/article/details/80150933

#include<bits/stdc++.h>
using namespace std;
struct Node{
    int data,left=-1,right=-1;
};
Node tree[105];
int data[105],N;
void inOrder(int root,int&index){//中根遍历
    if(root==-1)//root==-1,直接返回
        return;
    inOrder(tree[root].left,index);//递归遍历左子树
    tree[root].data=data[index++];//将数据填入根部结点
    inOrder(tree[root].right,index);//递归遍历右子树
}
void levelOrder(){//层次遍历
    queue<int>q;
    q.push(0);
    while(!q.empty()){
        int t=q.front();
        q.pop();
        printf("%s%d",t==0?"":" ",tree[t].data);
        if(tree[t].left!=-1)
            q.push(tree[t].left);
        if(tree[t].right!=-1)
            q.push(tree[t].right);
    }
}
int main(){
    scanf("%d",&N);
    for(int i=0;i<N;++i)
        scanf("%d%d",&tree[i].left,&tree[i].right);
    for(int i=0;i<N;++i)
        scanf("%d",&data[i]);
    sort(data,data+N);//排序
    int index=0;
    inOrder(0,index);
    levelOrder();
    return 0;
}
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#define INF 0x3f3f3f3f
using namespace std;
struct Node{
	int data = INF;int left,right;
	Node(){	data = INF;left=right=-1;	}
};
Node node[107];
int a[107];
int idx = 0;
void insert(int root){//中序遍历,同时完成赋值 
	if(node[root].left!=-1){
		insert(node[root].left);
	}
	node[root].data = a[idx];idx++;
	if(node[root].right!=-1){
		insert(node[root].right);
	} 
}
vector<Node> levelOrder(){//利用队列完成层次遍历 
	queue<Node> q;
	vector<Node> ans;
	q.push(node[0]);
	while(!q.empty()){
		Node cur = q.front();
		ans.push_back(cur);
		q.pop();
		if(cur.left!=-1) q.push(node[cur.left]);
		if(cur.right!=-1) q.push(node[cur.right]);
	}
	return ans;
}
int main(void){
	int N;cin>>N;
	for(int i=0;i<N;i++){
		cin>>node[i].left>>node[i].right;
	}
	for(int i=0;i<N;i++) cin>>a[i];
	sort(a,a+N); 
	insert(0);
	vector<Node> order = levelOrder();
	for(int i=0;i<order.size();i++){
		if(i==0) cout<<order[i].data;
		else cout<<" "<<order[i].data;
	}
	return 0;
}

 

实战指南:Qwen3-ASR-1.7B 语音识别 本文介绍了Qwen3-ASR语音识别模型的安装与使用。 阅读详情

相关推荐

Android GWP-Asan机制详解

基本用途:GWP-ASan是一种原生内存分配器功能,在内存分配时额外记录一些信息,用于检测DOUBLE_FREE、USE_AFTER_FREE、INVALID_FREE、BUFFER_OVERFLOW、BUFFER_UNDERFLOW五种内存错误问题。

专注于系统快稳省领域 4145

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

weixin_43893847的博客 175

谷歌浏览器崩溃排查:闪退、卡死、网页打不开的修复指南

浏览器多进程架构通过进程隔离、GPU加速和扩展沙箱提升网页渲染效率,但配置损坏、扩展冲突、显卡驱动异常或系统资源不足时,常会以闪退、卡死、白屏、内存占用飙升和能上微信却打不开网页等形式暴露。排查价值在于先分层定位系统、进程、配置扩展与网页内容,再用无痕模式、chrome://crashes、chrome://gpu和浏览器任务管理器收集证据,避免盲目重装。对办公、开发运维和日常维护场景,禁用扩展、关闭硬件加速、刷新DNS缓存、重置配置文件乃至干净重装,都是可复用的工程化处置手段。围绕谷歌浏览器崩溃的典型症状

weixin_34138056的博客 426

1099 Build A Binary Search Tree (30 point(s))

题解 建树 and bfs。 #include&lt;cstdio&gt; #include&lt;algorithm&gt; #include&lt;vector&gt; #include&lt;queue&gt; using namespace std; struct node { int v; int l, r; }; vector&lt;node&gt; t; vector&...

徐伯莱 241

海康视觉相机驱动软件MVS参数配置全攻略(附接线图+避坑指南)

本文提供了海康视觉相机驱动软件MVS从硬件连接到软件参数配置的完整指南。详细解析了供电与通讯线缆的区分、PNP/NPN信号逻辑的接线避坑方法,并重点阐述了MVS软件中触发模式、网络IP配置、图像采集及光源控制等核心参数的设置技巧,旨在帮助用户快速完成相机调试,实现稳定高效的图像采集。

weixin_29204205的博客 402

华为使用MicroG安装并使用Google服务教程

鉴于众多华为手机用户因无法使用Google服务而感到不便,我经过多次摸索和测试,总结了一份简单易懂的教程,希望能够帮助到大家。无论是需要使用Google Play商店下载应用,还是需要使用Google服务进行工作和学习,本文将详细介绍如何在华为手机上安装并使用Google服务。二、前置条件软件要求:Aurora Store(替代Google Play商店)、MicroG Companion、MicroG Server(替代Google框架)

weixin_41926602的博客 3万+

PAT_甲级_1099 Build A Binary Search Tree (30point(s)) (C++)【BST构建/层次遍历】

目录 1,题目描述 题目大意 2,思路 数据结构 算法 3,AC代码 4,解题过程 1,题目描述 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 4...

&再见萤火虫&的博客 303

DNA pull-down技术避坑指南:从探针设计到质谱验证的完整实验方案

本文提供了一份详尽的DNA pull-down技术实战避坑指南,涵盖从探针设计、生物素标记、磁珠选择与预处理,到结合洗脱优化及下游质谱验证或Western Blot验证的全流程。重点解析了如何通过精细操作降低非特异性结合、提高实验特异性与重复性,并对比了质谱与WB两种验证路径的适用场景,旨在帮助研究者构建稳健可靠的DNA-蛋白质互作研究体系。

weixin_29274337的博客 495

PAT甲级刷题记录——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 sub...

一只奋斗中的本科生 427

55、Mac OS X 10.2 安装、设置、卸载及故障排除全攻略

本博客全面解析了 Mac OS X 10.2 的安装、设置、卸载及故障排除方法。内容涵盖四种安装方式的适用场景及操作步骤、设置助手的详细配置流程、卸载注意事项与恢复建议,以及常见问题和进阶故障的解决办法。通过图文并茂的形式,帮助用户快速掌握 Mac OS X 10.2 的基础与高级操作,是使用该操作系统的实用指南。

qsc90123456的博客 151

RK3588, 绘制中文OSD,实现左右对齐

基于RK3588,实现中文OSD, 上下左右对齐。

J。 662

1099 Build A Binary Search Tree (30 point(s))

1099 Build A 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 with keys less than the node’s key. The right subtree of a n

佳迪斯的博客 205

双目深度相机--10.双目视觉的主要步骤讲解分析

双目视觉技术通过模拟人眼原理,利用两个相机从不同视角采集图像,实现三维坐标计算。其核心流程包括:1)系统标定,消除相机畸变并建立坐标映射关系;2)图像同步采集与预处理;3)立体匹配,寻找左右图像对应点;4)基于视差图的深度计算;5)三维重建生成点云或网格模型;6)结果应用如目标测距、三维建模等。关键技术难点在于精确标定和准确匹配,这两步直接影响最终深度计算的精度。该技术广泛应用于自动驾驶、工业检测、VR等领域,实现了从二维图像到三维空间的精准转换。

AIoT深度探索者 | 嵌入式系统架构师 深耕物联网(IoT)与人工智能(AI)融合技术,聚焦​​边缘计算、图像开发、低功耗通信协议​​,致力于智能硬件,关注我获取最新技术文章信息与海量资源~ 625

1099 Build A 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 with keys less than the node's key. The right su...

张蔡辉 271

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

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

锂电池充电器电路设计 里面有电路

锂电池充电器电路设计 里面有电路 3.7V升压到5V电路设计。

1099 Build A Binary Search Tree (30point(s)) - C语言 PAT 甲级

* 利用数组存储二叉搜索树 * 因为是二叉搜索树,节点的值序列排序后即为此树的中序遍历 * 中序遍历的过程中将节点值依次插入节点 * 再层序遍历一遍输出

有三只土豆 859

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

1099 Build A Binary Search Tree (30 point(s)) PAT甲级

1099 Build A Binary Search Tree (30 point(s)) BST,树的遍历 题目 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

neko的博客 121

PAT (甲级)1099 Build A Binary Search Tree (30point(s))

题目 题目链接 思路 题目大意:给一颗二叉树,并给一个序列,将这个序列填入二叉树中,使其构成二叉搜索树; 还是利用二叉搜索树的性质:中序遍历是元素从大到小排列的顺序;把给定序列排序即可得到中序; 中序遍历二叉树的同时填入数据,二叉树可以用静态链表来存储; 注意:树中序遍历的递归可以进行到叶子结点的下一层; 代码 #include <iostream> #include <vector> #include <stack> #include <queue> #in

hjyp的博客 228

PAT(A) 1099 Build A Binary Search Tree (30 point(s))

keys按升序排列,对已知结构BST填入值的过程即中序遍历 #include<bits/stdc++.h> using namespace std; struct node{ int left, right, value; }; int N, m = 0; vector<node> v; vector<int> key; void dfs(int index){ if(index == -1) return; dfs(v[index].lef

Anpotan的博客 104

1099 Build A Binary Search Tree——PAT甲级

思路:首先根据给出的结点建立二叉树本身,之后按照给出的数字排序后以中序遍历的方式建立二叉搜索树。build函数是建树,built函数是将二叉搜索树数据读入已有的树。

flyidj的博客 1131

【PAT甲级A1099】Build A Binary Search Tree (30分)(c++)

1099 Build A Binary Search Tree (30分) 作者:CHEN, Yue 单位:浙江大学 代码长度限制:16 KB 时间限制:200 ms 内存限制:64 MB 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 l

小庄同学的博客 211

PAT 甲级 1099  Build A Binary Search Tree

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

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

1099 Build A Binary Search Tree (30分)

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

韩江雪de 小屋 633

1099. Build A Binary Search Tree

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

sky247391475的博客 513

1099. Build A Binary Search Tree (30)

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

https://github.com/androidfans 778

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

chen_zan_yu_的博客 260

1099 Build A Binary Search Tree

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

qq_37617419的博客 195
上一篇: 1074 Reversing Linked List (25 point(s))
下一篇: 1106 Lowest Price in Supply Chain (25 point(s))
coderwait
博客等级 码龄9年 20粉丝 210原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值