当前位置:网站首页>二叉树专题--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;
}
边栏推荐
- Shell programming 01_ Shell foundation
- 【快应用】Win7系统使用华为IDE无法运行和调试项目
- 【AGC】如何解决事件分析数据本地和AGC面板中显示不一致的问题?
- js promise.all
- [TS] 1368 seconds understand typescript generic tool types!
- "Talking about podcasts" vol.352 the age of children: breaking the inner scroll, what can we do before high school?
- 面对不确定性,供应链的作用
- MYSQL关键字
- PCL eigen introduction and simple use
- [SUCTF2018]followme
猜你喜欢

4. Random variables

SUS系统可用性量表

Database dictionary Navicat automatic generation version

(五)APA场景搭建之挡位控制设置

12. Process synchronization and semaphore

618再次霸榜的秘密何在?耐克最新财报给出答案

Leetcode+ 76 - 80 storm search topic

618 what is the secret of dominating the list again? Nike's latest financial report gives the answer

1287_ Implementation analysis of prvtaskistasksuspended() interface in FreeRTOS

Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
随机推荐
长投学堂上面的账户安全吗?
PCL之K-d树与八叉树
SUS系统可用性量表
Point cloud projection picture
PCL之滤波
《MySQL 8 DBA基础教程》简介
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
Is this code PHP MySQL redundant?
js promise. all
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
axis设备的rtsp setup头中的url不能带参
简洁、快速、节约内存的Excel处理工具EasyExcel
Sus system availability scale
Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
华为快应用中如何实现同时传递事件对象和自定义参数
学习open62541 --- [66] UA_String的生成方法
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
最详细MySql安装教程
The URL in the RTSP setup header of the axis device cannot take a parameter
华为应用市场应用统计数据问题大揭秘