当前位置:网站首页>二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
二叉树专题--AcWing 3540. 二叉搜索树建树(实用板子 构建二叉搜索树 并输出前、中、后序遍历)
2022-07-02 07:21:00 【Morgannr】


题意:
输入一系列整数,利用所给数据建立一个二叉搜索树,并输出其前序、中序和后序遍历序列。
思路:
本题代码可以用做一个实用的板子。
我习惯用 结构体数组 bst 存树:
const int N = 110;
struct node
{
int key; //当前节点的键值
int l = -1, r = -1; //当前节点的左右儿子,先都初始化为 -1,表示还没有左右儿子
int cnt; //相同 key 值的节点有多少个
} bst[N];
按照 二叉搜索树的定义,递归构建一棵树,下面是 insert 函数:
void insert(int u, int key)
{
if (bst[0].key == 0) bst[idx++].key = key; //根节点键值特殊处理
if (key < bst[u].key)
{
if (bst[u].l == -1) //往左递归到不能再递归时,生成新的叶子结点并赋好左指针
{
bst[idx].key = key;
bst[u].l = idx++;
}
else insert(bst[u].l, key); //还没到尽头继续往左递归
}
else if (key > bst[u].key)
{
if (bst[u].r == -1) //往右递归到不能再递归时,生成新的叶子结点并赋好右指针
{
bst[idx].key = key;
bst[u].r = idx++;
}
else insert(bst[u].r, key); 还没到尽头继续往右递归
}
else if(key == bst[u].key) bst[u].cnt++;
}
接下来就是前、中、后序遍历的套路代码了:
前序 dfs1 函数:(根 左 右)
void dfs1(int u) //前序
{
if (u == -1) return; //到尽头回溯
ans1.push_back(bst[u].key); //根
dfs1(bst[u].l); //左
dfs1(bst[u].r); //右
}
中序 dfs2 函数:(左 根 右)
void dfs2(int u) //中续
{
if (u == -1) return;
dfs2(bst[u].l); //左
ans2.push_back(bst[u].key); //根
dfs2(bst[u].r); //右
}
后序 dfs3 函数:(左 右 根)
void dfs3(int u) //后续
{
if (u == -1) return;
dfs3(bst[u].l); //左
dfs3(bst[u].r); //右
ans3.push_back(bst[u].key); //根
}
代码:
#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 idx;
struct node
{
int key;
int l = -1, r = -1;
int cnt;
} bst[N];
int w[N];
int n;
vector<int> ans1, ans2, ans3;
void insert(int u, int key)
{
if (bst[0].key == 0) bst[idx++].key = key;
if (key < bst[u].key)
{
if (bst[u].l == -1)
{
bst[idx].key = key;
bst[u].l = idx++;
}
else insert(bst[u].l, key);
}
else if (key > bst[u].key)
{
if (bst[u].r == -1)
{
bst[idx].key = key;
bst[u].r = idx++;
}
else insert(bst[u].r, key);
}
else if(key == bst[u].key) bst[u].cnt++;
}
void dfs1(int u)//前序
{
if (u == -1) return;
ans1.push_back(bst[u].key);
dfs1(bst[u].l);
dfs1(bst[u].r);
}
void dfs2(int u)//中续
{
if (u == -1) return;
dfs2(bst[u].l);
ans2.push_back(bst[u].key);
dfs2(bst[u].r);
}
void dfs3(int u)//后续
{
if (u == -1) return;
dfs3(bst[u].l);
dfs3(bst[u].r);
ans3.push_back(bst[u].key);
}
signed main()
{
int T = 1; //cin >> T;
while (T--)
{
cin >> n;
for (int i = 0; i < n; ++i)
{
cin >> w[i];
insert(0, w[i]);
}
dfs1(0), dfs2(0), dfs3(0);
for (auto v : ans1) cout << v << ' ';
puts("");
for (auto v : ans2) cout << v << ' ';
puts("");
for (auto v : ans3) cout << v << ' ';
puts("");
}
return 0;
}
边栏推荐
- 13.信号量临界区保护
- 01-spooldir
- UVM - usage of common TLM port
- UVM learning - build a simple UVM verification platform
- Shapiro Wilk normal analysis by SPSS
- 《实习报告》Skywalking分布式链路追踪?
- Open the encrypted SQLite method with sqlcipher
- 618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
- Solution of mysql8 forgetting password file in Windows Environment
- LeetCode+ 76 - 80 暴搜专题
猜你喜欢
随机推荐
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
从.bag文件中读取并保存.jpg图片和.pcd点云
全网显示 IP 归属地,是怎么实现的?
Database dictionary Navicat automatic generation version
PCL 点云转深度图像
MySQL lethal serial question 3 -- are you familiar with MySQL locks?
首份中国企业敏捷实践白皮书发布| 附完整下载
快应用中实现自定义抽屉组件
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
[SUCTF2018]followme
Windows环境MySQL8忘记密码文件解决方案
PCL point cloud to depth image
数据库字典Navicat自动生成版本
使用华为性能管理服务,按需配置采样率
Operator-1初识Operator
点云投影图片
Operator-1 first acquaintance with operator
1287_FreeRTOS中prvTaskIsTaskSuspended()接口实现分析
UVM - configuration mechanism
2. Hacking lab script off [detailed writeup]









