1099. Build A Binary Search Tree (30)

MATLAB 与 STK 互联导出导弹轨迹及速度数据(csv导出) 本文介绍了如何通过 MATLAB 脚本连接 STK 提取导弹三维轨迹数据,并以 CSV 格式导出,包含以下关键内容:自动连接 STK 场景;获取所有导弹对象;提取 J2000 坐标系下的位置和速度;加入任务优先级与状态;导出为标准 CSV 文件; 阅读详情

1099. Build A Binary Search Tree (30)

时间限制
100 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=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)给出“固定结构”的二叉搜索树,然后给出树中的数组,要求还原这棵树;

(2)将数组从小到大排列正好是二叉搜索树的中序遍历结果,因此可以借此来还原整棵树;

(3)树还原之后,就可以利用队列轻松的进行层序遍历了;

这道题目关键还是在于上述的第二点。

写个中序遍历即可,只不过这次不是输出而是往里面填数,很不错的一道题

code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
vector<int> val;
int cnt = 0;
struct node{
    int val;
    int left;
    int right;
};
vector<node> tree;
void inorder(int root){
    if(root != -1){
        inorder(tree[root].left);
        tree[root].val = val[cnt++];
        inorder(tree[root].right);
    }
}
void levelorder(int root){
    queue<node> q;
    vector<node> ans;
    q.push(tree[root]);
    while(!q.empty()){
        node tmp = q.front();
        q.pop();
        ans.push_back(tmp);
        if(tmp.left != -1) q.push(tree[tmp.left]);
        if(tmp.right != -1) q.push(tree[tmp.right]);
    }
    for(int i = 0; i < ans.size(); i++){
        if(i == 0) printf("%d",ans[i].val);
        else printf(" %d",ans[i].val);
    }
    puts("");
}
int main(){
    int n;
    cin >> n;
    tree.resize(n);
    for(int i = 0; i < n; i++){
        cin >> tree[i].left >> tree[i].right;
    }
    val.resize(n);
    for(int i = 0; i < n; i++){
        cin >> val[i];
    }
    sort(val.begin(),val.end());
    inorder(0);
    levelorder(0);
    return 0;
}


C#联合Halcon机器视觉框架源码—升级版 相较于之前的NxtVision,本软件代码架构更加合理,且新增。对源码有需求的,订阅本专栏后,私信我领取。、原来的vb脚本改为。 阅读详情

相关推荐

博途WinCC上位机避坑指南:从工控机选型到趋势图优化的完整流程

本文详细解析了博途WinCC上位机开发全流程,从工控机选型到趋势图优化的关键技巧。针对工业自动化场景,提供硬件配置指南、网络优化方案和性能提升策略,帮助工程师避开常见陷阱,实现稳定高效的HMI系统部署。特别强调PC Station与传统触摸屏的选型对比及SIMATIC NET配置优化。

pp12345的博客 278

1099 Build A Binary Search Tree (30 分)

1099 Build A Binary Search Tree (30 分) 题意 给出一棵二叉树,n节点数,接下来n行代表每个节点(0-n-1)的两个子节点的编号,-1表示子节点不存在。最后给出n个数,要求把数填进该二叉树中,是二叉树成为二叉搜索树,输出二叉搜索树的层序序列。 思路 因为树有权值与子节点,可以用结构体数组建树。 因为二叉搜索树中序序列是单调不减,对n个数从小到大排序,对树进行中序遍历把数填进树的节点中(二叉搜索树中序遍历顺序对应数组中的数顺序遍历)。最后对树进行层序遍历,输出即可。 代码

weixin_43735634的博客 128

2025-08-19利用opencv检测图片中文字及图片的坐标

该代码实现了图片中中文文本的检测与位置计算功能。主要使用CnOcr库进行OCR识别,结合OpenCV和PIL库处理图像。功能包括:1)检测图片中的中文文本及位置坐标;2)计算每个文本的像素宽度和高度;3)在原图上绘制文本边界框并标注识别结果。代码通过put_chinese_text函数解决中文显示问题,最终输出各文本内容、坐标位置及尺寸信息。适用于需要获取图片中文字精确位置的应用场景。

m0_58149406的博客 394

1099 Build A Binary Search Tree(超详细注解+38行代码)

分数 30全屏浏览题目作者 CHEN, Yue单位 浙江大学。

m0_66329385的博客 849

STC51单片机串口多字节数据接收的优化策略

本文深入探讨了STC51单片机串口多字节数据接收的优化策略。针对数据丢包、粘包等常见痛点,核心思路是采用“缓冲队列+定时器超时”模式,将实时处理转变为“先收纳,后处理”。文章详细讲解了硬件初始化、中断服务程序优化(快速入队与激活超时裁判)以及主循环的非阻塞设计,并进阶介绍了环形缓冲区实现与错误处理,旨在构建稳定可靠的串口通信系统。

weixin_29264201的博客 374

运放+三极管恒流源电路设计实战:从原理到仿真一步到位(附Multisim文件)

本文详细解析了运放与三极管构建恒流源电路的核心原理,重点阐述了负反馈机制如何实现精准的电流控制。通过Multisim仿真实战,从电路搭建、参数计算到负载调整率与动态响应分析,一步步指导设计过程,并提供常见故障的调试技巧与PCB设计要点,帮助工程师从理论平稳过渡到可靠实践。

7z8x2c4v6b9的博客 711

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 nodes with keys less than the node's key.The right subtre...

qq_27013285的博客 239

MATLAB实战:5步搞定GNSS单点定位解算(附完整代码)

本文提供了一份基于MATLAB的GNSS单点定位实战指南。通过5个核心步骤,详细讲解了如何从卫星坐标和伪距观测值出发,利用最小二乘迭代算法,构建并实现一个完整的单点定位解算器。文章附有完整代码,并涵盖精度评估与结果分析,适合GNSS初学者和MATLAB用户快速上手实践。

a2b3c4d5e的博客 250

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

计算机毕设项目十四:美团酒店评论情感分析完整数据代码-可直接运行

计算机毕设项目十四:美团酒店评论情感分析完整数据代码-可直接运行

693

评估篇| 大模型评测综述

1、基于人类的评测。与针对基础大语言模型的自动化评测不同,微调大语言模型的评测更加注重模型在实际应用场景中的表现,如与人类交互的自然度、对齐度等。2、基于模型的评测。例如,AlpacaEval 排行榜 基于由大语言模型合成的人类需求指令作为评测任务,然后收集待评估大模型的回应,并采用 GPT-4 等大语言模型作为评测员,将待评估大语言模型的输出与参考输出进行成对比较。1、大语言模型对评估设置极为敏感,包括问题的表述方式、提示样本的选择以及答案的解析策略等,这些细微的差别都可能导致评估结果的显著变化。

youbingchen的博客 636

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

雪之下 297

lvgl 9虚拟键盘控件 (lv_keyboard) 使用指南

是 LittlevGL 提供的一个虚拟键盘控件,适用于在图形界面中输入文本。与按钮矩阵类似,虚拟键盘预定义了多个键位布局,支持多种模式和自定义键位映射,能够满足各种文本输入需求。通过与文本区域 (Text Area) 的结合,键盘能够自动将输入的字符传送到指定区域。控件是 LittlevGL 提供的强大虚拟键盘解决方案,适用于任何需要文本输入的界面。通过预定义的键盘模式、自定义键位映射和文本区域自动填充功能,可以大大提升用户输入体验。无论是在嵌入式系统的用户界面中,还是在其他需要虚拟键盘的应用中,

m0_62599305的博客 2185

【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

此去经年 1894

廖雪峰Python教程[完整版]

廖雪峰的python教程整理的,已经排好版了,而且还是完整的,适合小白

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

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

大中型超市收银系统(可直接运行)

光步前台收银系统系统功能:商品销售、商品退货、挂单、收银员权限、会员销售、单品取消、整单取消、商品查询、脱机销售、脱机数据上传,基本资料下载,开钱箱、小票打印、收款结算,支持多币种付款方式(现金、储值卡、礼券、微信等),支持多种促销。运行环境:.net 4.0登陆信息用户名:1001 密码:123456内置商品信息,商品编码可输入00001000 - 00010000,测试效果相关功能键F5 结算 F9挂单 F11商品查询 F8退货 Insert单品取消 Del整单取消 F12开钱箱F2 会员 Ctrl+Z退出系统 Home 锁屏 相关源码待公开

【1099】Build A Binary Search Tree (30 分)

#include<iostream> #include<stdio.h> #include<stdlib.h> #include<math.h> #include<string.h> #include<algorithm> #include<map> #include<vector> #inclu...

发现问题,并解决问题,批判性思维 679

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

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

qq_37378303的博客 263

1099 Build A Binary Search Tree (30)

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

2401_88085478的博客 967

PAT 1099 Build A Binary Search Tree (30)

1099 Build A Binary Search Tree (30)(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 ...

ryo_218的博客 291
上一篇: L2-013. 红色警报
下一篇: 1076. Forwards on Weibo (30)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值