当前位置:网站首页>二叉树专题--AcWing 1589. 构建二叉搜索树
二叉树专题--AcWing 1589. 构建二叉搜索树
2022-07-02 07:21:00 【Morgannr】



题意:
规定 0 号节点为根节点,题目给出 各个编号节点之间的父子关系,以及 一系列不同的权值。
依据 二叉搜索树的定义,将给出的不同权值 插入并构建出二叉搜索树。
要求输出构建出的 二叉搜索树的层序遍历 (BFS 序)
思路:
我们知道,一棵二叉搜索树的 中序遍历一定是有序的,因此,我们肯定先要将题中 给定的一系列权值先排好序。
排好序后,按照树的中序遍历的顺序 将所有权值 填入树中 即可。
最后,输出其宽度优先遍历的序列 即可。
大体思路就是这样子,下面谈谈 代码细节。
对于 二叉搜索树中每个节点,我更习惯采用类似于线段树存储节点的方式,用一个 结构体数组 bst。
const int N = 110;
struct node
{
int val;
int l, r;
} bst[N];
结构体中,val 代表当前节点权值,l 代表左儿子编号,r 代表右儿子的编号(即 左右指针)
将输入的 权值数组 w 排好序后,我们就可以 使用 dfs 按照中序遍历的顺序 进行建树了(将 w 数组填入树中节点)。
从 w 数组 中 下标为 k = 0 的位置 开始填
如何完成 dfs 的中序遍历:
- 如果 当前点
u = -1,表示为 空节点。直接return回溯。 - 否则,先递归左子树
dfs(bst[u].l, k);后递归右子树dfs(bst[u].r, k); - 在 递归左、右子树之间 时,我们需要 遍历当前点,即
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);
}
完事后,对原树进行 宽度优先遍历,以 0 号点为根节点 进行遍历:(注意:边遍历边输出)
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("");
}
时间复杂度:
O ( n ) O(n) O(n)
代码:
#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;
}
边栏推荐
- (五)APA场景搭建之挡位控制设置
- Analysis of hot spots in AI technology industry
- UVM - configuration mechanism
- Mysql database remote access permission settings
- shell编程01_Shell基础
- From Read and save in bag file Jpg pictures and PCD point cloud
- Shutter - canvas custom graph
- 13. Semaphore critical zone protection
- 从.bag文件中读取并保存.jpg图片和.pcd点云
- js setTimeout()与面试题
猜你喜欢

Flutter——Canvas自定义曲线图

Flink calculates topn hot list in real time
![[SUCTF2018]followme](/img/63/9104f9c8bd24937b0fc65053efec96.png)
[SUCTF2018]followme

Overview of integrated learning

【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?

【TS】1368- 秒懂 TypeScript 泛型工具类型!

Hdu1234 door opener and door closer (water question)

JSP webshell免杀——webshell免杀

13. Semaphore critical zone protection
![2. Hacking lab script off [detailed writeup]](/img/f3/29745761cd5ad4df84c78ac904ea51.png)
2. Hacking lab script off [detailed writeup]
随机推荐
Set the playback speed during the playback of UOB equipment
华为AppLinking中统一链接的创建和使用
Windows环境MySQL8忘记密码文件解决方案
12. Process synchronization and semaphore
13. Semaphore critical zone protection
Open the encrypted SQLite method with sqlcipher
Thanos Receiver
Overview of integrated learning
Learn open62541 -- [66] UA_ Generation method of string
从MediaRecord录像中读取H264参数
2022爱分析· 国央企数字化厂商全景报告
【TS】1368- 秒懂 TypeScript 泛型工具类型!
01 install virtual machine
【快应用】Win7系统使用华为IDE无法运行和调试项目
How to get the password of cpolar?
Oracle notes
2022-06-17
Database dictionary Navicat automatic generation version
HDU1234 开门人和关门人(水题)
转换YV12到RGB565图像转换,附YUV转RGB测试