当前位置:网站首页>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;
}边栏推荐
- sql语句模糊查询遇到的问题
- JDBC database operation
- Silent authorization login and registration of wechat applet
- MySQL winter vacation self-study 2022 12 (3)
- Problems encountered in fuzzy query of SQL statements
- Actual combat 8051 drives 8-bit nixie tube
- @RequestMapping
- 【SQL注入】联合查询(最简单的注入方法)
- [research materials] 2022q1 game preferred casual game distribution circular - Download attached
- Market status and development prospect prediction of the global fire hose industry in 2022
猜你喜欢

Uipath practice (08) - selector
![[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)](/img/34/d195752992f8955bc2a41b4ce751db.jpg)
[set theory] relationship properties (symmetry | symmetry examples | symmetry related theorems | antisymmetry | antisymmetry examples | antisymmetry theorems)

论文阅读_清华ERNIE

Valentine's day limited withdrawal guide: for one in 200 million of you

Kept hot standby and haproxy

ZABBIX monitoring of lamp architecture (3): zabbix+mysql (to be continued)

Mobile terminal - uniapp development record (public request encapsulation)
![[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached](/img/95/833f5ec20207ee5d7e6cdfa7208c5e.jpg)
[research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached

Introduction to JVM principle

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
随机推荐
The 19th Zhejiang I. barbecue
Keepalived热备与HAProxy
Learn to use the idea breakpoint debugging tool
第十九届浙江省 I. Barbecue
[PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
Analysis of proxy usage of ES6 new feature
Wechat applet distance and map
1115 counting nodes in a BST (30 points)
@RequestMapping
Shuttle + Alluxio 加速内存Shuffle起飞
Market status and development prospect prediction of global fermented plant protein industry in 2022
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
Number of uniform strings of leetcode simple problem
Web security - CSRF (token)
Leetcode simple question: check whether two string arrays are equal
112 stucked keyboard (20 points)
Market status and development prospect prediction of global colorimetric cup cover industry in 2022
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
STM32 reverse entry