当前位置:网站首页>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;
}
边栏推荐
- 3588. 排列与二进制
- @Solution to DS ('slave') multi data source compatible transaction problem
- Foundation of knowledge atlas (II) - knowledge expression system of knowledge atlas
- JS learning notes 24-28: end
- linear transformation
- Mysql使用left join连表查询时,因连接条件未加索引导致查询很慢
- Compose learning notes 1-compose, state, flow, remember
- Development status of security and privacy computing in China
- What is the difference between UTF-8, utf-16 and UTF-32 character encoding? [graphic explanation]
- 2021-09-02
猜你喜欢

边缘技术和小程序容器在智能家居中的应用

Knowledge map Foundation (I) - what is knowledge map

Repvgg paper explanation and model reproduction using pytoch

汇编学习

SQL labs detailed problem solving process (less1-less10)

即刻体验 | 借助 CTS-D 进一步提升应用设备兼容性
Node.js+express realizes the operation of MySQL database

Xiaobai can understand the 35 necessary questions in MySQL interview

Untitled may

Enterprise wechat customer service link, enterprise wechat customer service chat
随机推荐
拓展运算符是深拷贝还是浅拷贝
shellcode编写(未完)
18、 ROS topic name setting
Introduction to mqtt protocol
3511. Water pouring problem
3477. 简单排序
Keras reported an error using tensorboard: cannot stop profiling
Instant experience | further improve application device compatibility with cts-d
DataTables warning: table id=campaignTable - Cannot reinitialise DataTable.解决
@Solution to DS ('slave') multi data source compatible transaction problem
JS常用的3种弹出框
3540. 二叉搜索树
Gradle -- package multiple variants with gradle
SQL learning
Rocky基础之修改网卡名为eth0
Instructions for common symbols in kotlin
JS -- realize the rotation chart (complete function)
PHP memory horse
How to gracefully encapsulate anonymous inner classes (DSL, high-order functions) in kotlin
SQL labs detailed problem solving process (less1-less10)