当前位置:网站首页>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;
}边栏推荐
- Market status and development prospects of the global autonomous marine glider industry in 2022
- Thesis reading_ Chinese NLP_ ELECTRA
- JDBC database operation
- Notes | numpy-11 Array operation
- Actual combat 8051 drives 8-bit nixie tube
- 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
- Hire cashier (differential constraint)
- Silent authorization login and registration of wechat applet
- First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
- 7. Integrated learning
猜你喜欢

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

RT thread flow notes I startup, schedule, thread

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

7. Integrated learning

Unity tool Luban learning notes 1

并发操作-内存交互操作

Truncated sentences of leetcode simple questions

Source insight garbled code solution

Number of uniform strings of leetcode simple problem

Thesis reading_ Chinese medical model_ eHealth
随机推荐
Thesis reading_ Tsinghua Ernie
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
带有注意力RPN和多关系检测器的小样本目标检测网络(提供源码和数据及下载)...
论文阅读_清华ERNIE
7. Integrated learning
Market status and development prospect prediction of the global fire hose industry in 2022
Market status and development prospect prediction of the global fire alarm sensor industry in 2022
Shell script Basics - basic grammar knowledge
SSM framework integration
Shuttle + alluxio accelerated memory shuffle take-off
Handler understands the record
[XSS bypass - protection strategy] understand the protection strategy and better bypass
[luatos sensor] 2 air pressure bmp180
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
Market status and development prospect prediction of global SoC Test Platform Industry in 2022
[research materials] annual report of China's pension market in 2021 - Download attached
第十九届浙江省 I. Barbecue
Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
【PHP漏洞-弱类型】基础知识、php弱相等、报错绕过
Sprintf formatter abnormal exit problem