当前位置:网站首页>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;
}
边栏推荐
- Hire cashier (differential constraint)
- The reason why the entity class in the database is changed into hump naming
- 并发操作-内存交互操作
- Online VR model display - 3D visual display solution
- 1118 birds in forest (25 points)
- [research materials] 2022q1 game preferred casual game distribution circular - Download attached
- [clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
- Notes | numpy-07 Slice and index
- Market status and development prospect forecast of global heat curing adhesive industry in 2022
- Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
猜你喜欢
Analysis of proxy usage of ES6 new feature
Keepalived热备与HAProxy
论文阅读_清华ERNIE
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
Learn to use the idea breakpoint debugging tool
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
Silent authorization login and registration of wechat applet
Cross platform plug-in flutter for displaying local notifications_ local_ notifications
Leetcode simple problem delete an element to strictly increment the array
【XSS绕过-防护策略】理解防护策略,更好的绕过
随机推荐
Leetcode simple question: check whether the string is an array prefix
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
Current market situation and development prospect forecast of the global fire boots industry in 2022
Truncated sentences of leetcode simple questions
【SQL注入点】注入点出现位置、判断
论文阅读_中文医疗模型_ eHealth
[SQL injection] joint query (the simplest injection method)
MPM model and ab pressure test
雇佣收银员(差分约束)
Problems encountered in fuzzy query of SQL statements
Notes | numpy-07 Slice and index
RT thread flow notes I startup, schedule, thread
Basic use of Metasploit penetration testing framework
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
The least operation of leetcode simple problem makes the array increment
论文阅读_ICD编码_MSMN
1114 family property (25 points)
[research materials] 2021 China's game industry brand report - Download attached