当前位置:网站首页>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;
}边栏推荐
- 雇佣收银员(差分约束)
- Online VR model display - 3D visual display solution
- Source insight garbled code solution
- [research materials] annual report of China's pension market in 2021 - Download attached
- MPM model and ab pressure test
- Thesis reading_ Chinese NLP_ ELECTRA
- [develop wechat applet local storage with uni app]
- Learn to use the idea breakpoint debugging tool
- Preparation for school and professional cognition
- 1115 counting nodes in a BST (30 points)
猜你喜欢

Sdl2 + OpenGL glsl practice (Continued)

JDBC database operation

7. Integrated learning

Cross platform plug-in flutter for displaying local notifications_ local_ notifications
![[luatos sensor] 2 air pressure bmp180](/img/88/2a6caa5fec95e54e3fb09c74ba8ae6.jpg)
[luatos sensor] 2 air pressure bmp180
![[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa](/img/a9/92059db74ccde03b84c69dfce35b37.jpg)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa

Leetcode simple question: check whether two string arrays are equal
![[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)](/img/36/53886b9d3b98f744be2b6aa6b5d3eb.jpg)
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)

Handler understands the record

Actual combat 8051 drives 8-bit nixie tube
随机推荐
[SQL injection] joint query (the simplest injection method)
Learn to use the idea breakpoint debugging tool
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
Notes | numpy-10 Iterative array
Wechat applet distance and map
The process of browser accessing the website
The programmer resigned and was sentenced to 10 months for deleting the code. JD came home and said that it took 30000 to restore the database. Netizen: This is really a revenge
Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
112 stucked keyboard (20 points)
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
Blog building tool recommendation (text book delivery)
Silent authorization login and registration of wechat applet
50 practical applications of R language (36) - data visualization from basic to advanced
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
String matching: find a substring in a string
The reason why the entity class in the database is changed into hump naming
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过