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:

9
25 30 42 16 20 20 35 -5 28

Sample Output:

2 + 4 = 6
 #include<cstdio>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
using namespace std;
typedef struct NODE{
struct NODE *lchild, *rchild;
int data;
int level;
}node;
void insert(node* &root, int x){
if(root == NULL){
node* temp = new node;
temp->lchild = NULL;
temp->rchild = NULL;
temp->data = x;
root = temp;
return;
}
if(x <= root->data)
insert(root->lchild, x);
else insert(root->rchild, x);
}
int depth = , n1, n2;
void levelOrder(node* root){
queue<node*> Q;
root->level = ;
Q.push(root);
while(Q.empty() == false){
node* temp = Q.front();
depth = temp->level;
Q.pop();
if(temp->lchild != NULL){
temp->lchild->level = temp->level + ;
Q.push(temp->lchild);
}
if(temp->rchild != NULL){
temp->rchild->level = temp->level + ;
Q.push(temp->rchild);
}
}
}
void DFS(node * root){
if(root == NULL)
return;
if(root->level == depth)
n1++;
else if(root->level == depth -)
n2++;
DFS(root->lchild);
DFS(root->rchild);
}
int main(){
int N, temp;
node* root = NULL;
scanf("%d", &N);
for(int i = ; i < N; i++){
scanf("%d", &temp);
insert(root, temp);
}
levelOrder(root);
DFS(root);
printf("%d + %d = %d", n1, n2, n1 + n2);
cin >> N;
return ;
}

总结:

1、注意最下面两层,和叶节点+叶节点上一层节点个数是不一样的。

A1115. Counting Nodes in a BST的更多相关文章

  1. PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  2. PAT甲级——A1115 Counting Nodes in a BST【30】

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  3. PAT_A1115#Counting Nodes in a BST

    Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursiv ...

  4. 1115 Counting Nodes in a BST (30 分)

    1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...

  5. PAT1115:Counting Nodes in a BST

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  6. PAT甲1115 Counting Nodes in a BST【dfs】

    1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...

  7. [二叉查找树] 1115. Counting Nodes in a BST (30)

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. PAT 1115 Counting Nodes in a BST[构建BST]

    1115 Counting Nodes in a BST(30 分) A Binary Search Tree (BST) is recursively defined as a binary tre ...

  9. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

随机推荐

  1. css 别人找的css特效

    https://blog.csdn.net/m0_37809478/article/details/76619207

  2. K3CLOUD常用数据表

    一.数据库查询常用表 --查询数据表select * from ( select convert(varchar(4000),t1.FKERNELXML.query('//TableName')) a ...

  3. snv的使用

    1.搭建SVN服务器 (1)直接安装 (2)创建工号,分组,分配权限(图形化界面的直接操作,非图形界面的需要改配置文件conf文件夹下) (3)创建仓库,D:\Repositories\OA:cmd命 ...

  4. python之路--JavaScript

    一. JavaScript概述 ECMAScript和JavaScript的关系 1996年11月,JavaScript的创造者--Netscape公司,希望这门语言能成为国际化标准,于是决定将Jav ...

  5. 使用python库xlsxwriter库来输出各种xlsx文件

    功能性的文章直接用几个最简单的实现表达: xlsxwriter库的核心就是其Workbook对象. 创建一个指定名字的xlsx文件: import xlsxwriter filename = '/Us ...

  6. Python 版百度站长平台链接主动推送脚本

    如果自己的网站需要被百度收录,可以在搜索结果中找到,就需要将网站的链接提交给百度.依靠百度的爬虫可能无法检索到网站所有的内容,因此可以主动将链接提交给百度. 在百度的站长平台上介绍了链接提交方法,目前 ...

  7. div中的相对定位与绝对定位

    1.position:relative; 如果对一个元素进行相对定位,首先它将出现在它所在的位置上.然后通过设置垂直或水平位置,让这个元素“相对于”它的原始起点进行移动.(再一点,相对定位时,无论是否 ...

  8. Python学习之路—————day04

    今日内容: 1. 循环语句 1.1 if判断 1.2 while循环 1.3 for循环 一.if判断 语法一: if 条件 代码块1 代码块2 代码块3 # 例: sex='female' age= ...

  9. Unable to resolve target 'android-15'

    SDK 15没有加载造成的,在SDK Manager.exe下安装以下文件 Android SDK Tools (25.2.5) Android SDK Platform-tools (28.0.1) ...

  10. Spring Boot 构建电商基础秒杀项目 (二) 使用 Spring MVC 方式获取用户信息

    SpringBoot构建电商基础秒杀项目 学习笔记 修改 DOMapper 在 UserPasswordDOMapper.xml 添加: <select id="selectByUse ...