当前位置:网站首页>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;
}
边栏推荐
- MySQL environment configuration
- Win11 arm系统配置.net core环境变量
- Easyexcel, a concise, fast and memory saving excel processing tool
- Retrofit's callback hell is really vulnerable in kotlin synergy mode!
- AI技术产业热点分析
- The URL in the RTSP setup header of the axis device cannot take a parameter
- UWA report uses tips. Did you get it? (the fourth bullet)
- MySQL keyword
- 最详细MySql安装教程
- Kustomize user manual
猜你喜欢
随机推荐
(五)APA场景搭建之挡位控制设置
Pywin32 opens the specified window
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
拆解美图SaaS:开着飞机换引擎
二叉树专题--AcWing 1589. 构建二叉搜索树
PCL extracts a subset from a point cloud
Introduction to MySQL 8 DBA foundation tutorial
MySQL数据库远程访问权限设置
PCL point cloud to depth image
Easyexcel, a concise, fast and memory saving excel processing tool
[TS] 1368 seconds understand typescript generic tool types!
Set the playback speed during the playback of UOB equipment
长投学堂上面的账户安全吗?
软件产品管理系统有哪些?12个最佳产品管理工具盘点
从MediaRecord录像中读取H264参数
【ARK UI】HarmonyOS ETS的启动页的实现
Hdu1234 door opener and door closer (water question)
如何用list组件实现tabbar标题栏
Open the encrypted SQLite method with sqlcipher
[SUCTF2018]followme








