当前位置:网站首页>1099 build a binary search tree (30 points)
1099 build a binary search tree (30 points)
2022-07-03 04:55:00 【vs5】
The main idea of the topic : Each node of the tree is determined by children , Insert the given value so that it is a binary search tree .
Flattening the binary search tree is orderly , That is, the middle order traversal is ordered . According to this nature , Go through it in middle order , Save node number and depth , After sorting, the hierarchy traversal ( Sort by depth and number )
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
struct node
{
int l,r,h,idx,val;
}e[200];
vector<node>ans;
vector<int>v(200);
int n,cnt;
void dfs(int u,int h,int idx)
{
if(e[u].l != -1) dfs(e[u].l,h + 1,idx << 1);
ans.push_back({0,0,h,idx,v[cnt ++]});// Traverse in middle order
if(e[u].r != -1) dfs(e[u].r,h + 1,idx << 1 | 1);
}
int main()
{
cin >> n;
for(int i = 0; i < n; i ++) cin >> e[i].l >> e[i].r;
for(int i = 0; i < n; i ++) cin >> v[i];
sort(v.begin(),v.begin() + n);
dfs(0,0,1);
sort(ans.begin(),ans.end(),[](node a,node b)
{
if(a.h != b.h) return a.h < b.h;return a.idx < b.idx;
});
for(int i = 0; i < ans.size(); i ++)
{
if(i != 0) cout << ' ';
cout << ans[i].val;
}
return 0;
}
边栏推荐
- Basic use of Metasploit penetration testing framework
- Thesis reading_ Tsinghua Ernie
- Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
- Flutter monitors volume to realize waveform visualization of audio
- 关于开学的准备与专业认知
- 1114 family property (25 points)
- Thesis reading_ Chinese medical model_ eHealth
- JDBC database operation
- Sdl2 + OpenGL glsl practice (Continued)
- [research materials] annual report of China's pension market in 2021 - Download attached
猜你喜欢
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
JDBC database operation
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
并发操作-内存交互操作
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
【XSS绕过-防护策略】理解防护策略,更好的绕过
[USACO 2009 Dec S]Music Notes
随机推荐
General undergraduate college life pit avoidance Guide
Current market situation and development prospect forecast of the global fire boots industry in 2022
[luatos sensor] 1 light sensing bh1750
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
Career planning of counter attacking College Students
Learning record of arouter principle
sql语句模糊查询遇到的问题
[luatos sensor] 2 air pressure bmp180
普通本科大学生活避坑指南
[BMZCTF-pwn] 18-RCTF-2017-Recho
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip
The least operation of leetcode simple problem makes the array increment
【XSS绕过-防护策略】理解防护策略,更好的绕过
Do you know UVs in modeling?
1114 family property (25 points)
Notes | numpy-11 Array operation
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)