当前位置:网站首页>二叉树专题--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;
}
边栏推荐
- C#中索引器
- Record attributeerror: 'nonetype' object has no attribute 'nextcall‘
- Pywin32 opens the specified window
- 2022-06-17
- Retrofit's callback hell is really vulnerable in kotlin synergy mode!
- 简洁、快速、节约内存的Excel处理工具EasyExcel
- Lunix reallocates root and home space memory
- "Matching" is true love, a new attitude for young people to make friends
- JSP webshell免杀——JSP的基础
- Oracle 笔记
猜你喜欢

VSCode工具使用

(5) Gear control setting of APA scene construction

Sus system availability scale

首份中国企业敏捷实践白皮书发布| 附完整下载

MySQL environment configuration

UVM learning - build a simple UVM verification platform

Shapiro Wilk normal analysis by SPSS

Read H264 parameters from mediarecord recording

Leetcode+ 76 - 80 storm search topic

HDU1234 开门人和关门人(水题)
随机推荐
互联网快讯:腾讯会议应用市场正式上线;Soul赴港递交上市申请书
华为AppLinking中统一链接的创建和使用
Win11 arm系统配置.net core环境变量
Overview of integrated learning
JSP webshell免殺——JSP的基礎
Filtering of PCL
MYSQL关键字
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
点云投影图片
SPSS做Shapiro-Wilk正态分析
《MySQL 8 DBA基础教程》简介
P1055 [NOIP2008 普及组] ISBN 号码
数据库字典Navicat自动生成版本
Lunix reallocates root and home space memory
华为联机对战服务玩家掉线重连案例总结
Kustomize使用手册
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
Transport Optimization abstraction
UWA report uses tips. Did you get it? (the fourth bullet)
13.信号量临界区保护