当前位置:网站首页>3540. Binary search tree
3540. Binary search tree
2022-07-28 15:13:00 【Pursue people far away】
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][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
Code :
#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int w[N], l[N], r[N], idx;
int n;
int root;
void add(int u, int x)
{
if (x < w[u])
{
if (!l[u])
l[u] = ++idx, w[idx] = x;
else
add(l[u], x);
}
else if (x > w[u])
{
if (!r[u])
r[u] = ++idx, w[idx] = x;
else
add(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;
if (root == 0)
root = 1, w[root] = x, ++idx;
else
add(root, x);
}
for (int i = 0; i < 3; i++)
{
dfs(root, i);
cout << endl;
}
return 0;
}
边栏推荐
- Solve blast database error: error pre fetching sequence data
- Basic operation implementation of sequence table
- Pytorch GPU installation
- 2021 year end summary of gains and losses
- Repvgg paper explanation and model reproduction using pytoch
- The difference between @notnull, @notblank, @notempty of commonly used verification annotations
- Knowledge map Foundation (I) - what is knowledge map
- JS常用的3种弹出框
- Wonderful frog -- how simple can it be to abandon the float and use the navigation bar set by the elastic box
- 3540. 二叉搜索树
猜你喜欢

Chapter II linear table

PS modify the length and width pixels and file size of photos

Knowledge map Foundation (I) - what is knowledge map

When MySQL uses left join to query tables, the query is slow because the connection conditions are not properly guided

Hard disk partition method
![What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]](/img/a9/336390db64d871fa1655800c1e0efc.png)
What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]

安装mosek,license安装位置查找

No files or folders found to process

JS学习笔记18-23

Introduction to mqtt protocol
随机推荐
RepVGG论文详解以及使用Pytorch进行模型复现
3477. Simple sorting
Application of edge technology and applet container in smart home
3715. 最少交换次数
When MySQL uses left join to query tables, the query is slow because the connection conditions are not properly guided
A problem -- about dragging div in JS, when I change div to round, there will be a bug
Slider restore and validation (legal database)
linear transformation
9、 C array explanation
Privacy computing summary
Touch hands to realize canal how to access Mysql to realize data write operation monitoring
JS study notes 18-23
Chapter I Introduction
shellcode编写学习-环境
iframe 标签
JS -- realize the rotation chart (complete function)
Downloading PIP package is too slow
Chapter 3 stack, queue and array
软件开发三大痛点!小程序容器如何解决?
PHP magic method