当前位置:网站首页>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;
}
边栏推荐
- Idea commonly used 10 shortcut keys
- Bug --- redis deserialization failed
- "Everyday Mathematics" serial 61: March 1
- JS how to delete an element without deleting its child elements
- Daily question brushing record (XXVIII)
- NFT: how to improve rentable NFT (erc-4907)
- (2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
- Android interview question: why do activities rebuild ViewModel and still exist—— Jetpack series (3)
- New discovery of ROS callback function
- R language Visual scatter diagram, geom using ggrep package_ text_ The repl function avoids overlapping labels between data points (set the hJust parameter to show that labels of all data points are a
猜你喜欢

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

Unity accesses chartandgraph chart plug-in

HTB-Beep

Big talk · book sharing | Haas Internet of things device cloud integrated development framework

Leetcode 204. count prime numbers (wonderful)
![[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node](/img/06/b90fa310158669696f94f79ef4cc5a.png)
[node] the service port is occupied error: listen eaddinuse: address already in use::: 9000- how to close the port started by node

Sword finger offer 05. replace spaces

Amazoncaptcha 95%成功率绕过亚马逊IP验证码
![[daily practice] day (14)](/img/83/d924fa0fc5ae01d151a0880da62f7e.png)
[daily practice] day (14)

Xiaomi 12s UTRA Leica watermark generation online tool
随机推荐
R language uses LM function to build multiple linear regression model and write regression equation according to model coefficient
Mechanism and principle of multihead attention and masked attention
(2022牛客多校二)L-Link with Level Editor I(动态规划)
Pdf snapshot artifact
10. Rendering Basics
Sword finger offer 54. the k-th node of the binary search tree
Adaptation dynamics | in June, sequoiadb completed mutual certification with five products
(2022 Niuke multi School II) k-link with bracket sequence I (dynamic planning)
EOL offline sequence based on iso13209 (Otx)
sqlilabs less-28~less-8a
leetcode/二进制加法
剑指 Offer 36. 二叉搜索树与双向链表
2021 ICPC Shaanxi warm up match b.code (bit operation)
2020icpc Jiangxi warm up e.robot sends red packets (DFS)
Analyzing the principle of DNS resolution in kubernetes cluster
(2022年牛客多校一)I-Chiitoitsu(期望DP)
Common methods of JS operation array
An SQL execution process
[QT] solve the problem of Chinese garbled code output from QT console
Linear algebra (3)