当前位置:网站首页>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;
}边栏推荐
- 1115 counting nodes in a BST (30 points)
- Oracle SQL table data loss
- 文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
- Three representations of signed numbers: original code, inverse code and complement code
- Review the configuration of vscode to develop golang
- sql语句模糊查询遇到的问题
- 逆袭大学生的职业规划
- 1118 birds in forest (25 points)
- Truncated sentences of leetcode simple questions
- Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
猜你喜欢

Leetcode simple problem delete an element to strictly increment the array

5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip

论文阅读_清华ERNIE

The reason why the entity class in the database is changed into hump naming

Thesis reading_ Chinese medical model_ eHealth

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 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

Truncated sentences of leetcode simple questions

Analysis of proxy usage of ES6 new feature
随机推荐
Shell script Basics - basic grammar knowledge
Leetcode simple question: the key with the longest key duration
1119 pre- and post order traversals (30 points)
【工具跑SQL盲注】
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
[tools run SQL blind note]
Notes | numpy-10 Iterative array
Games101 Lesson 9 shading 3 Notes
Concurrent operation memory interaction
ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)
Compile and decompile GCC common instructions
Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
Distinguish between releases and snapshots in nexus private library
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
The 19th Zhejiang I. barbecue
Objects. Requirenonnull method description
2022-02-11 daily clock in: problem fine brush
Network security textual research recommendation
Market status and development prospects of the global autonomous marine glider industry in 2022