当前位置:网站首页>Counting Nodes in a Binary Search Tree
Counting Nodes in a Binary Search Tree
2022-07-27 05:01:00 【Small l ~~~】
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
The left subtree of a node contains only nodes with keys less than or equal to the node’s key.
The right subtree of a node contains only nodes with keys greater than the node’s key.
Both the left and right subtrees must also be binary search trees.
Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤1000) which is the size of the input sequence. Then given in the next line are the N integers in [−1000,1000] which are supposed to be inserted into an initially empty binary search tree.
Output Specification:
For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:
n1 + n2 = n
where n1 is the number of nodes in the lowest level, n2 is that of the level above, and n is the sum.
Sample Input:
10
25 30 42 16 20 20 35 -5 28 22
Sample Output:
3 + 4 = 7
The main idea of the topic : Build a binary sort tree according to the given sequence , Then output the number of nodes in the bottom two layers .
#include<bits/stdc++.h>
using namespace std;
typedef struct Tree {
int data;
int level; // Number of layers where nodes are located
struct Tree *lchild, *rchild;
}bstnode, *bstree;
int n, a[1010];
int height = 0; // Tree height
int sum1 = 0, sum2 = 0;
void BST_insert(bstree &T, int k, int cnt) {
height = max(height, cnt);
if(T == NULL) {
T = (bstree) malloc (sizeof(bstnode));
T -> data = k;
T -> level = cnt;
T -> lchild = T -> rchild = NULL;
}
else if(k <= T -> data) BST_insert(T -> lchild, k, cnt + 1);
else BST_insert(T -> rchild, k, cnt + 1);
}
void Create_BST(bstree &T) {
T = NULL;
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("%d", &a[i]);
BST_insert(T, a[i], 1);
}
}
void inorder(bstree T) {
if(T == NULL) return;
inorder(T -> lchild);
//cout << T -> data << " ";
if(T -> level == height) sum1++;
if(T -> level == height - 1) sum2++;
inorder(T -> rchild);
}
int main() {
bstree tree;
Create_BST(tree);
inorder(tree);
printf("%d + %d = %d", sum1, sum2, sum1 + sum2);
return 0;
}
边栏推荐
- The usage syntax and scene of selector, as well as the usage of background picture size, text box shadow and excessive effect
- HCIA static routing comprehensive experiment
- Review of various historical versions of Photoshop and system requirements
- 接口和抽象类/方法学习 demo
- CDH cluster integration external Flink (improved version - keep pace with the times)
- Sunset red warm tone tinting filter LUTS preset sunset LUTS 1
- 【搜索】—— 多源BFS + 最小步数模型
- Explanation of index failure principle and its common situations
- ps太卡怎么办?几步帮您解决问题
- R-score reproduction R-Precision evaluation index quantitative text generation image r-score quantitative experiment whole process reproduction (R-Precision) quantitative evaluation experiment step on
猜你喜欢

TCP three handshakes and four disconnects

Idea 如何新建一个groovy的项目(图文详细解释)

QT 菜单栏、工具栏和状态栏

Digital integrated circuit: CMOS inverter (I) static characteristics

Find a specific number in an ordered array

Photoshop裁剪工具隐藏技巧

Approval of meeting OA

STL 上头系列——list 容器详解

项目对接支付宝支付,内网穿透实现监听支付宝的支付成功异步回调通知
![[C language] detailed explanation of user-defined types (structure + enumeration + Union)](/img/d9/b10371159c63c126b5ff98bac0971a.png)
[C language] detailed explanation of user-defined types (structure + enumeration + Union)
随机推荐
How to copy Photoshop layers to other documents
日落红暖色调调色滤镜luts预设Sunset LUTs 1
【搜索】Flood Fill 和 最短路模型
Replication of df-gan experiment -- detailed steps of replication of dfgan and forwarding from remote port to local port using mobaxtem
如何做数据平滑迁移:双写方案
2019 top tennis cup upload
数字中国建设峰会闭幕,现场海量图片一览!
STM32 cubeMX HAL-----PWM—改变频率
「Photoshop2021入门教程」“拉平”带有透视感的图像
事件的接受与忽略
STM32_ HAL_ SUMMARY_ NOTE
"Photoshop2021 tutorial" align and distribute to make dot patterns
Digital integrated circuit: CMOS inverter (I) static characteristics
HCIA static routing comprehensive experiment
Customize the viewport height, and use scrolling for extra parts
C中文件I/O的使用
vim的基本操作
安装Pygame
Knapsack problem DP
Complete Binary Tree