当前位置:网站首页>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;
}
边栏推荐
- (14)[驱动开发]配置环境 VS2019 + WDK10 写 xp驱动
- How to play a data mining game entry Edition
- Softing pngate series gateway: integrate PROFIBUS bus into PROFINET network
- npx和npm区别
- 2020ICPC 江西省赛热身赛 E.Robot Sends Red Packets(dfs)
- 基于ISO13209(OTX)实现EOL下线序列
- sqlilabs less-28~less-8a
- Xiaomi 12s UTRA Leica watermark generation online tool
- Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络
- R language uses LM function to build multiple linear regression model and write regression equation according to model coefficient
猜你喜欢

(Niuke multi School II) G-LINK with monotonic subsequence (construction question)
![[daily practice] day (14)](/img/83/d924fa0fc5ae01d151a0880da62f7e.png)
[daily practice] day (14)
![Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure](/img/78/2a4e9d49bee8ef839d9d86fc7c3c83.png)
Get URL of [url reference]? For the following parameters, there are two ways to get the value of the corresponding parameter name and convert the full quantity to the object structure

剑指 Offer 36. 二叉搜索树与双向链表

Sword finger offer 05. replace spaces

Why is it that all the games are pseudorandom and can't make true random?

Baidu, Alibaba, Tencent, who fell first?
![(14) [driver development] configuration environment vs2019 + wdk10 write XP driver](/img/90/0d94d26be8128d77de65919763fda5.png)
(14) [driver development] configuration environment vs2019 + wdk10 write XP driver

Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)

New discovery of ROS callback function
随机推荐
Base64 (conversion between string and Base64 string)
R language uses data.table function to create data.table data (use: operator to create continuous numeric vector)
Sword finger offer 32 - I. print binary tree from top to bottom
How to play a data mining game entry Edition
Big talk · book sharing | Haas Internet of things device cloud integrated development framework
Working principle and precautions of bubble water level gauge
Sword finger offer 54. the k-th node of the binary search tree
Unity Animator动画与状态机
How to start if you want to be a product manager?
Adaptation dynamics | in June, sequoiadb completed mutual certification with five products
Softing pnGate系列网关:将PROFIBUS总线集成到PROFINET网络
Atof(), atoi(), atol() functions [detailed]
R language uses rowmedians function to calculate the row data median value of all data rows in dataframe
线性代数(三)
QT qtextedit setting qscrollbar style sheet does not take effect solution
剑指 Offer 45. 把数组排成最小的数
Differences and application directions of GPS, base station and IP positioning
HTB-Arctic
Amazoncaptcha 95%成功率绕过亚马逊IP验证码
Unity animator animation and state machine