当前位置:网站首页>Binary search tree (day 75)
Binary search tree (day 75)
2022-07-25 06:02:00 【Zhangxueheng】
List of articles
1: subject
Enter a series of integers , Use the given data to build a binary search tree , And output its preamble 、 Middle order and post order traversal sequences .
Input format
The first line is an integer n, Indicates the number of input integers .
The second line contains n It's an integer .
Output format
There are three lines , The first line outputs the preorder traversal sequence , The second line outputs the middle order traversal sequence , The third line outputs the post order traversal sequence .
There may be duplicate elements in the input , But the repeated elements in the output binary tree traversal sequence do not need to be output .
Data range
1≤n≤100,
Input element value range [1,1000].
sample input :
5
1 6 5 9 8
sample output :
1 6 5 9 8
1 5 6 8 9
5 8 9 6 1
2: Code implementation
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
int n;
int l[N], r[N], w[N], idx;
int root;
void insert(int& u, int x)
{
if (!u) u = ++ idx, w[u] = x;
else if (x < w[u]) insert(l[u], x);
else if (x > w[u]) insert(r[u], x);
}
void dfs(int u, int t)
{
if (!u) return;
if (t == 0) cout << w[u] << ' ';
dfs(l[u], t);
if (t == 1) cout << w[u] << ' ';
dfs(r[u], t);
if (t == 2) cout << w[u] << ' ';
}
int main()
{
cin >> n;
while (n -- )
{
int x;
cin >> x;
insert(root, x);
}
for (int i = 0; i < 3; i ++ )
{
dfs(root, i);
cout << endl;
}
return 0;
}
边栏推荐
- Design of automatic machine dot drawing script based on C language
- QT qtextedit setting qscrollbar style sheet does not take effect solution
- leetcode/ 前 n 个数字二进制中 1 的个数
- R language obtains the data row where the nth maximum value of the specified data column is located in the data.table data
- [daily practice] day (14)
- 剑指 Offer 54. 二叉搜索树的第k大节点
- NFT: how to improve rentable NFT (erc-4907)
- Leetcode 0121. the best time to buy and sell stocks - simulation from back to front
- ERA5数据集说明
- Concepts of phase velocity and phase in transmission line theory
猜你喜欢

同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网

Difference between NPX and NPM

HTB-Arctic

The computer accesses the Internet normally with the same network cable, and the mobile phone connects to WiFi successfully, but it cannot access the Internet

(Niuke multi School II) j-link with arithmetic progress (least square method / three points)

QT qtextedit setting qscrollbar style sheet does not take effect solution

Prometheus operator configures promethesrule alarm rules

剑指 Offer 54. 二叉搜索树的第k大节点

Idea commonly used 10 shortcut keys

50 places are limited to open | with the news of oceanbase's annual press conference coming!
随机推荐
50 places are limited to open | with the news of oceanbase's annual press conference coming!
Daily question brushing record (XXVIII)
HTB-Optimum
Programming hodgepodge (II)
Unity Animator动画与状态机
Era5 dataset description
(14) [driver development] configuration environment vs2019 + wdk10 write XP driver
leetcode/ 前 n 个数字二进制中 1 的个数
sqlilabs less-28~less-8a
线性代数(三)
Calculate BDP value and wnd value
同条网线电脑正常上网,手机连接wifi成功,但是无法访问互联网
(Niuke multi school I in 2022) i-chiitoitsu (expected DP)
How to play a data mining game entry Edition
2020icpc Jiangxi warm up e.robot sends red packets (DFS)
Unity model simplification / consolidation one click plug-in
Blocking Queue Analysis
Matlab drawing example: 5: Biaxial graph
CCID released the "Lake warehouse integrated technology research report", and Jushan database was selected as a typical representative of domestic enterprises
VO, dto, do, Po distinction and use