当前位置:网站首页>Special topic of binary tree -- acwing 1589 Building binary search tree
Special topic of binary tree -- acwing 1589 Building binary search tree
2022-07-02 10:58:00 【Morgannr】



The question :
Regulations 0 Node number is the root node , The title gives The parent-child relationship between each numbered node , as well as A series of different weights .
basis Definition of binary search tree , Different weights will be given Insert and build a binary search tree .
Require output to build Sequence traversal of binary search tree (BFS order )
Ideas :
We know , Of a binary search tree The middle order traversal must be ordered , therefore , We must first put the question in A given series of weights are arranged in order .
After arranging the order , According to the middle order of the tree Set the ownership value Fill in the tree that will do .
Last , Output the sequence whose width first traversal that will do .
The general idea is like this , Let's talk about Code details .
about Each node in the binary search tree , I'm more used to using a method similar to the segment tree storage node , Use one Array of structs bst.
const int N = 110;
struct node
{
int val;
int l, r;
} bst[N];
In the structure ,val Represents the weight of the current node ,l Represents the number of the left son ,r The number representing the right son ( namely Left and right pointer )
Will input Weight array w After arranging the order , We can Use dfs In the order of traversal It's time to build a tree ( take w The array fills the nodes in the tree ).
from w Array in Subscript to be k = 0 The location of Start filling in
How to complete dfs Middle order traversal of :
- If Current point
u = -1, Expressed as Blank nodes . directreturnto flash back . - otherwise , First recursive left subtree
dfs(bst[u].l, k);Post recursive right subtreedfs(bst[u].r, k); - stay Recursive left 、 Between right subtrees when , We need to Traverse the current point , namely
bst[u].val = w[k++];
void dfs(int u, int& k)
{
if (u == -1) return;
dfs(bst[u].l, k);
bst[u].val = w[k++];
dfs(bst[u].r, k);
}
When it's done , The original tree Breadth first traversal , With 0 Point number is the root node Traversal :( Be careful : Output while traversing )
void bfs(int u)
{
queue<int> q; q.push(u);
cout << bst[u].val << ' ';
while (q.size())
{
int tt = q.front(); q.pop();
if (bst[tt].l != -1) {
q.push(bst[tt].l);
cout << bst[bst[tt].l].val << ' ';
}
if (bst[tt].r != -1) {
q.push(bst[tt].r);
cout << bst[bst[tt].r].val << ' ';
}
}
puts("");
}
Time complexity :
O ( n ) O(n) O(n)
Code :
#define _CRT_SECURE_NO_WARNINGS 1
#include <bits/stdc++.h>
using namespace std;
//#define int long long
//#define map unordered_map
const int N = 110;
int n;
struct node
{
int val;
int l, r;
} bst[N];
int w[N];
void dfs(int u, int& k)
{
if (u == -1) return;
dfs(bst[u].l, k);
bst[u].val = w[k++];
dfs(bst[u].r, k);
}
void bfs(int u)
{
queue<int> q; q.push(u);
cout << bst[u].val << ' ';
while (q.size())
{
int tt = q.front(); q.pop();
if (bst[tt].l != -1) {
q.push(bst[tt].l);
cout << bst[bst[tt].l].val << ' ';
}
if (bst[tt].r != -1) {
q.push(bst[tt].r);
cout << bst[bst[tt].r].val << ' ';
}
}
puts("");
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> bst[i].l >> bst[i].r;
}
for (int i = 0; i < n; ++i)
{
cin >> w[i];
}
sort(w, w + n);
int k = 0;
dfs(0, k);
bfs(0);
}
return 0;
}
边栏推荐
猜你喜欢

Retrofit's callback hell is really vulnerable in kotlin synergy mode!

AI技术产业热点分析

1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析

Analysis of hot spots in AI technology industry

Use WinDbg to statically analyze dump files (summary of practical experience)
![[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)](/img/9f/4265f1e3927fcf66602f0fc9e7a9d9.jpg)
[visual studio] visual studio 2019 community version cmake development environment installation (download | install relevant components | create compilation execution project | error handling)

Mongodb quickly get started with some simple operations of mongodb command line
![二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)](/img/c2/bb85b681af0f78b380b1d179c7ea49.png)
二叉树专题--洛谷 P3884 [JLOI2009]二叉树问题(dfs求二叉树深度 bfs求二叉树宽度 dijkstra求最短路)

【AGC】构建服务3-认证服务示例

二叉树专题--AcWing 18. 重建二叉树(利用前、中序遍历,构建二叉树)
随机推荐
Set the playback speed during the playback of UOB equipment
MYSQL关键字
二叉树专题--AcWing 1589. 构建二叉搜索树
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
PCL 从一个点云中提取一个子集
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Open the encrypted SQLite method with sqlcipher
Kustomize使用手册
计算序列之和
From Read and save in bag file Jpg pictures and PCD point cloud
The URL in the RTSP setup header of the axis device cannot take a parameter
【付费推广】常见问题合集,推荐榜单FAQ
static 函数中的静态变量
2022爱分析· 国央企数字化厂商全景报告
快应用中实现自定义抽屉组件
Oracle notes
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
集成学习概览
【ARK UI】HarmonyOS ETS的启动页的实现