当前位置:网站首页>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 . directreturn
to 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;
}
边栏推荐
- 学习open62541 --- [66] UA_String的生成方法
- 12. Process synchronization and semaphore
- Easyexcel, a concise, fast and memory saving excel processing tool
- Overview of integrated learning
- 全网显示 IP 归属地,是怎么实现的?
- [TS] 1368 seconds understand typescript generic tool types!
- From Read and save in bag file Jpg pictures and PCD point cloud
- Pywin32 opens the specified window
- 【ARK UI】HarmonyOS ETS的启动页的实现
- 618再次霸榜的秘密何在?耐克最新财报给出答案
猜你喜欢
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Dialogue Wu Gang: why do I believe in the rise of "big country brands"?
Kustomize使用手册
从MediaRecord录像中读取H264参数
UVM learning - object attribute of UVM phase
使用sqlcipher打开加密的sqlite方法
Session cookies and tokens
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
13.信号量临界区保护
随机推荐
软件产品管理系统有哪些?12个最佳产品管理工具盘点
2. Hacking lab script off [detailed writeup]
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
洛谷 P5536 【XR-3】核心城市(贪心 + 树形 dp 寻找树的中心)
MongoDB 学习整理(条件操作符,$type 操作符,limit()方法,skip() 方法 和 sort() 方法)
UVM learning - object attribute of UVM phase
axis设备的rtsp setup头中的url不能带参
JSP webshell free -- the basis of JSP
[SUCTF2018]followme
js数组常用方法
The nanny level tutorial of flutter environment configuration makes the doctor green to the end
Ks009 implement pet management system based on SSH
Hdu1236 ranking (structure Sorting)
Point cloud projection picture
SPSS做Shapiro-Wilk正态分析
二叉树专题--AcWing 1497. 树的遍历(利用后、中序遍历,构建二叉树)
13. Semaphore critical zone protection
转换YV12到RGB565图像转换,附YUV转RGB测试
2.hacking-lab脚本关[详细writeup]
2022-06-17