当前位置:网站首页>二叉树专题--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;
}
边栏推荐
- Jsp webshell Free from killing - The Foundation of JSP
- Thanos Receiver
- AppGallery Connect场景化开发实战—图片存储分享
- (五)APA场景搭建之挡位控制设置
- 【AppLinking实战案例】通过AppLinking分享应用内图片
- P1055 [noip2008 popularization group] ISBN number
- MYSQL环境配置
- Sus system availability scale
- Operator-1 first acquaintance with operator
- JSP webshell free -- webshell free
猜你喜欢
Internet News: Tencent conference application market was officially launched; Soul went to Hong Kong to submit the listing application
js数组常用方法
Open the encrypted SQLite method with sqlcipher
2.hacking-lab脚本关[详细writeup]
Nodejs+express+mysql simple blog building
Shapiro Wilk normal analysis by SPSS
618 what is the secret of dominating the list again? Nike's latest financial report gives the answer
JSP webshell free -- webshell free
一招快速实现自定义快应用titlebar
Session cookies and tokens
随机推荐
SPSS做Shapiro-Wilk正态分析
UWA report uses tips. Did you get it? (the fourth bullet)
PCL 点云转深度图像
MYSQL关键字
From Read and save in bag file Jpg pictures and PCD point cloud
Analysis of hot spots in AI technology industry
集成学习概览
HDU1234 开门人和关门人(水题)
"Matching" is true love, a new attitude for young people to make friends
PCL extracts a subset from a point cloud
6种单例模式的实现方式
Transport Optimization abstraction
JSP webshell free -- the basis of JSP
MySQL lethal serial question 4 -- are you familiar with MySQL logs?
使用sqlcipher打开加密的sqlite方法
Disassembling Meitu SaaS: driving the plane to change the engine
对话吴纲:我为什么笃信“大国品牌”的崛起?
JSP webshell免杀——JSP的基础
Win11 arm系统配置.net core环境变量
Mongodb quickly get started with some simple operations of mongodb command line