当前位置:网站首页>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;
}
边栏推荐
- Knowledge map Foundation (I) - what is knowledge map
- Three pain points of software development! How to solve the applet container?
- Downloading PIP package is too slow
- Why do enterprises need user autonomous digital identity
- 【游戏测试工程师】初识游戏测试—你了解它吗?
- Basic operation implementation of sequence table
- 3438. Number system conversion
- svg 验证码识别体验
- 3559. Ring counting
- Node.js+express realizes the operation of MySQL database
猜你喜欢

经典Dijkstra与最长路

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

Establishment and traversal of binary tree (implemented in C language)

Image steganography method

iPhone苹果手机上一些不想让他人看到的APP应用图标怎么设置手机桌面上的APP应用设置隐藏不让显示在手机桌面隐藏后自己可以正常使用的方法?

Use of beefs

Feeling about software development work in the second anniversary

MLX90640 红外热成像仪传感器模块开发笔记(八)

Find papers and their open source code

从thinkphp远程代码执行学php反射类
随机推荐
Establishment and traversal of binary tree (implemented in C language)
4518. Minimum ticket price
Hard disk partition method
PHP memory horse
云上安全主要面临的威胁有哪些
List of security technologies to be considered in cloud computing
Is the expansion operator a deep copy or a shallow copy
18、 ROS topic name setting
Chapter 3 stack, queue and array
Analysis vulnerability introduction
chrome插件调试
3559. Ring counting
Compose learning notes 2 - launchedeffect, status and status management
Partition and index of Oracle Database
3715. Minimum number of exchanges
Solution to the problem of high collapse caused by float (including all methods)
9、 C array explanation
Using keras to visualize the network model, failed to import pydot appears
SQL learning
3540. 二叉搜索树