当前位置:网站首页>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;
}
边栏推荐
- 使用mq消息队列来进行下单流程的高并发设计,消息挤压,消息丢失,消息重复的产生场景和解决方案
- Comprehensive experiment of static routing
- Read write separation and master-slave synchronization
- C中文件I/O的使用
- Sunset red warm tone tinting filter LUTS preset sunset LUTS 1
- Photoshop各历史版本回顾以及系统要求
- 【无标题】按照一定的条件,对 i 进行循环累加。条件通常为循环数组的长度,当超过长度就停止循环。因为对象无法判断长度,所以通常搭配 Object.keys() 使用。\nforEach 一般认为是 普
- Two way republication experiment
- 2019 top tennis cup upload
- "Photoshop2021 introductory tutorial" flatten "perspective images
猜你喜欢

strlen和sizeof的区别

Plato farm is expected to further expand its ecosystem through elephant swap

.htaccess学习

Easily download data in power Bi reports with power auto

2022 T2i text generated image Chinese Journal Paper quick view-1 (ecagan: text generated image method based on channel attention mechanism +cae-gan: text generated image technology based on transforme

网络协议详解:IP

How to import PS style? Photoshop style import tutorial

What if Photoshop prompts that the temporary storage disk is full? How to solve the problem that PS temporary storage disk is full?

Comprehensive experiment of static routing

【报错】Cannot read property ‘parseComponent‘ of undefined
随机推荐
事件(event)
Stm32 cubemx hal----- PWM - change frequency
Huawei's entry into the commercial market: due to the trend, there are many challenges
The usage syntax and scene of selector, as well as the usage of background picture size, text box shadow and excessive effect
对话框简介
Final Cut Pro Chinese tutorial (2) understanding of material window
When using Photoshop, the prompt "script error -50 general Photoshop error appears“
OFDM 十六讲 2- OFDM and the DFT
ps怎么导入lut预设?Photoshop导入lut调色预设教程
STM32 cubeMX HAL-----PWM—改变频率
The digital China Construction Summit ended with a list of massive pictures on site!
Basic configuration of static routing to achieve network wide accessibility
【搜索】双向广搜 + A*
Error: cannot read properties of undefined (reading 'then')
Cache read / write policies: cacheside, read/writethrough and writeback policies
安装Pygame
Photoshop各历史版本回顾以及系统要求
二叉搜索树详讲
测试基础5
【报错】Cannot read property ‘parseComponent‘ of undefined