当前位置:网站首页>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
边栏推荐
猜你喜欢

STM32使用HAL库,整体结构和函数原理介绍

让地球少些“碳”息 度能在路上

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

STM32 uses Hal library. The overall structure and function principle are introduced

Idea is configured to connect to the remote database mysql, or Navicat fails to connect to the remote database (solved)

Let the earth have less "carbon" and rest on the road

idea底栏打开services

洋桃电子STM32物联网入门30步笔记三、新建CubeIDE工程和设置讲解

信息收集相关知识点及题解

SYS_CONNECT_BY_PATH(column,'char') 结合 start with ... connect by prior
随机推荐
swagger文档导出自定义v2/api-docs拦截
Shell脚本进阶
QT reads all files under the path or files of the specified type (including recursion, judging whether it is empty and creating the path)
form中enctype属性
让地球少些“碳”息 度能在路上
GUI编程简介 swing
关于cin,scanf和getline,getchar,cin.getline的混合使用
Introduction to protobuf
Use of Arthas in JVM tools
Yangtao electronic STM32 Internet of things entry 30 step notes IV. engineering compilation and download
Star Trek强势来袭 开启元宇宙虚拟与现实的梦幻联动
Redis master-slave server problem
Go语言自学系列 | golang嵌套结构体
《深度学习》学习笔记(八)
mycat配置
STM32使用HAL库,整体结构和函数原理介绍
洋桃电子STM32物联网入门30步笔记四、工程编译和下载
vmware 搭建ES8的常见错误
[C语言] 文件操作《一》
信息收集相关知识点及题解