当前位置:网站首页>1099 建立二叉搜索树 (30 分)
1099 建立二叉搜索树 (30 分)
2022-04-23 08:35:00 【怀化第二深情】
- 节点的左侧子树仅包含键小于节点键的节点。
- 节点的右侧子树仅包含键大于或等于节点键的节点。
- 左子树和右子树也必须是二叉搜索树。
给定二叉树的结构和一系列不同的整数键,只有一种方法可以将这些键填充到树中,以便生成的树满足BST的定义。您应该输出该树的级别顺序遍历序列。图 1 和图 2 说明了该示例。

输入规格:
每个输入文件包含一个测试用例。对于每种情况,第一行给出一个正整数N (≤100),这是树中的节点总数。下一个N行中的每一个都包含一个节点的左子级和右子级,格式为 ,前提是节点的编号从 0 到left_index right_indexN−1,0 始终是根。如果一个孩子失踪了,那么−1将表示 NULL 子指针。最后N不同的整数键在最后一行给出。
输出规格:
对于每个测试用例,在一行中打印该树的级别顺序遍历序列。所有数字必须用空格分隔,行尾不能有多余的空格。
示例输入:
<span style="color:#404040"><span style="background-color:#ffffff"><code class="language-in">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
</code></span></span>
示例输出:
58 25 82 11 38 67 45 73 42
#include<cstdio>
#include<vector>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
typedef long long ll;
const int N = 1e5+10;
map<int,int>l,r;
int kp[10010];
int res[10010];
int u=0;
void dfs(int t){
if(l[t]!=-1)dfs(l[t]);//最小的安排在左边
res[t]=kp[u++];//这里差不多是中位数了
if(r[t]!=-1)dfs(r[t]);
}
void print(){
queue<int>p;
p.push(0);
int num=0;
while(p.size()){
int t=p.front();
if(!num++)cout<<res[t];
else cout<<' '<<res[t];
p.pop();
if(l.count(t)&&l[t]!=-1)p.push(l[t]);
if(r.count(t)&&r[t]!=-1)p.push(r[t]);
}
}
int main(){
int n;
cin>>n;
int x,y;
for(int i=0;i<n;i++){
cin>>x>>y;
l[i]=x;
r[i]=y;
}
for(int i=0;i<n;i++){
cin>>kp[i];
}
sort(kp,kp+n);
dfs(0);
print();
return 0;
}
版权声明
本文为[怀化第二深情]所创,转载请带上原文链接,感谢
https://blog.csdn.net/dege2929512534/article/details/124307022
边栏推荐
- cadence的工艺角仿真、蒙特卡洛仿真、PSRR
- Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
- PgSQL wants to implement all kinds of column sub query operations of MySQL
- Ajax cache prevention method
- Test your machine learning pipeline
- Large amount of data submitted by form post
- xctf刷题小记
- input元素添加监听事件
- HAL库的RCC简介
- QFileDialog select multiple files or folders
猜你喜欢

'bully' Oracle enlarged its move again, and major enterprises deleted JDK overnight...

SYS_ CONNECT_ BY_ Path (column, 'char') combined with start with connect by prior

ELK生产实践

Harbor企业级镜像管理系统实战

On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched

vmware 搭建ES8的常见错误

洋桃电子STM32物联网入门30步笔记二、CubeIDE下载、安装、汉化、设置

Notes on 30 steps of introduction to the Internet of things of yangtao electronics STM32 III. cubemx graphical programming and setting the IO port on the development board

Transformer XL: attention language modelsbbeyond a fixed length context paper summary

pgsql想实现mysql一样样的列子查询操作
随机推荐
Protobuf简介
okcc呼叫中心外呼系统智能系统需要用多大的盘存录音?
Let the earth have less "carbon" and rest on the road
记录:js删除数组中某一项或几项的几种方法
GUI编程简介 swing
PDF with watermark
经典题目刷一刷
Go语言自学系列 | golang结构体作为函数参数
Swagger document export custom V2 / API docs interception
Misunderstanding of flush () method of OutputStream class
Copy array in JS
虚拟线上展会-线上vr展馆实现24h沉浸式看展
On time atom joins hands with oneos live broadcast, and the oneos system tutorial is fully launched
对OutputStream类的flush()方法的误解
How much inventory recording does the intelligent system of external call system of okcc call center need?
Basic usage of synchronized locks
【精品】利用动态代理实现事务统一管理 二
Go语言自学系列 | golang方法
匿名類型(C# 指南 基礎知識)
'恶霸' Oracle 又放大招,各大企业连夜删除 JDK。。。