当前位置:网站首页>1099 establish binary search tree (30 points)
1099 establish binary search tree (30 points)
2022-04-23 08:41:00 【Huaihua second affectionate】
- The left subtree of a node contains only nodes whose key is less than the node key .
- The right subtree of a node contains only nodes whose key is greater than or equal to the node key .
- The left and right subtrees must also be binary search trees .
Given the structure of a binary tree and a series of different integer keys , There is only one way to populate these keys into the tree , So that the generated tree can meet BST The definition of . You should output the level order traversal sequence of the tree . chart 1 Sum graph 2 This example is illustrated .

Enter specifications :
Each input file contains a test case . For each case , The first line gives a positive integer N (≤100), This is the total number of nodes in the tree . next N Each row contains the left and right children of a node , The format is , The premise is that the node number is from 0 To left_index right_indexN−1,0 Always the root . If a child is missing , that −1 Will represent NULL Sub pointer . Last N Different integer keys are given on the last line .
Output specifications :
For each test case , Print the level order traversal sequence of the tree in one line . All numbers must be separated by spaces , There can't be extra spaces at the end of the line .
Sample input :
<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>
Sample output :
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]);// The smallest arrangement is on the left
res[t]=kp[u++];// This is almost the median
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;
}
版权声明
本文为[Huaihua second affectionate]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230835222858.html
边栏推荐
猜你喜欢

excle加水印

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

面了一圈,整理了这套面试题。。

正点原子携手OneOS直播 OneOS系统教程全面上线

洋桃电子STM32物联网入门30步笔记四、工程编译和下载

Flink SQL实现流批一体
![Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]](/img/67/1f9df4236b0aac3480836d45ab8561.png)
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]

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

RPC procedure

数据可视化:使用Excel制作雷达图
随机推荐
洋桃電子STM32物聯網入門30步筆記一、HAL庫和標准庫的區別
求简单类型的矩阵和
Ajax cache prevention method
form表单 post提交 数据量大的问题
PgSQL wants to implement all kinds of column sub query operations of MySQL
匿名類型(C# 指南 基礎知識)
作文以记之 ~ 二叉树的前序遍历
After a circle, I sorted out this set of interview questions..
LaTeX论文排版操作
根据后序和中序遍历输出先序遍历 (25 分)
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
关于堆的判断 (25 分) 两种插入方式
Input / output system
扣缴义务人
synchronized 锁的基本用法
Ear acupoint diagnosis and treatment essay 0421
第一性原理 思维导图
增强现实技术是什么?能用在哪些地方?
Go语言自学系列 | golang结构体的初始化
idea打包 jar文件