当前位置:网站首页>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;
}
边栏推荐
猜你喜欢
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
Shutter - canvas custom graph
Nodejs+express+mysql simple blog building
如何使用IDE自动签名调试鸿蒙应用
Jsp webshell Free from killing - The Foundation of JSP
二叉树专题--AcWing 3384. 二叉树遍历(已知先序遍历 边建树 边输出中序遍历)
Introduction to MySQL 8 DBA foundation tutorial
4. Random variables
Common methods of JS array
Shapiro Wilk normal analysis by SPSS
随机推荐
首份中国企业敏捷实践白皮书发布| 附完整下载
From Read and save in bag file Jpg pictures and PCD point cloud
Thanos Receiver
Shell programming 01_ Shell foundation
QT学习日记8——资源文件添加
What is the significance of the college entrance examination
软件产品管理系统有哪些?12个最佳产品管理工具盘点
如何使用IDE自动签名调试鸿蒙应用
Session cookies and tokens
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
HDU1228 A + B(map映射)
P1055 [NOIP2008 普及组] ISBN 号码
Operator-1 first acquaintance with operator
最详细MySql安装教程
UVM——Callback
js数组常用方法
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
Shutter - canvas custom graph
PCL之滤波
对话吴纲:我为什么笃信“大国品牌”的崛起?