当前位置:网站首页>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;
}边栏推荐
- Why does I start with =1? How does this code work?
- [PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
- MediaTek 2023 IC written examination approved in advance (topic)
- Leetcode simple question: the key with the longest key duration
- 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
- Silent authorization login and registration of wechat applet
- The process of browser accessing the website
- Shuttle + alluxio accelerated memory shuffle take-off
- 1114 family property (25 points)
- Thesis reading_ ICD code_ MSMN
猜你喜欢

C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement

Games101 Lesson 9 shading 3 Notes

Sdl2 + OpenGL glsl practice (Continued)

论文阅读_中文医疗模型_ eHealth

Interface frequency limit access
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750

移动端——uniapp开发记录(公共请求request封装)

Number of 1 in binary (simple difficulty)
![[USACO 2009 Dec S]Music Notes](/img/e6/282a8820becdd24d63dcff1b81fcaf.jpg)
[USACO 2009 Dec S]Music Notes
![[research materials] 2021 China's game industry brand report - Download attached](/img/b7/a377b0b7c742078e2feb28ebfbca62.jpg)
[research materials] 2021 China's game industry brand report - Download attached
随机推荐
SSM framework integration
[BMZCTF-pwn] 18-RCTF-2017-Recho
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
《牛客刷verilog》Part II Verilog进阶挑战
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
Why does I start with =1? How does this code work?
Realize file download through the tag of < a > and customize the file name
2022-02-11 daily clock in: problem fine brush
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
Objects. Requirenonnull method description
Three representations of signed numbers: original code, inverse code and complement code
第十九届浙江省 I. Barbecue
[SQL injection point] location and judgment of the injection point
Learn to use the idea breakpoint debugging tool
MediaTek 2023 IC written examination approved in advance (topic)
Market status and development prospect prediction of global fermentation acid industry in 2022
移动端——uniapp开发记录(公共请求request封装)
Market status and development prospects of the global IOT active infrared sensor industry in 2022
Blog building tool recommendation (text book delivery)