A1115. Counting Nodes in a BST
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的更多相关文章
- 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 ...
- 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 ...
- PAT_A1115#Counting Nodes in a BST
Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursiv ...
- 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 ...
- PAT1115:Counting Nodes in a BST
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
随机推荐
- DNS 到底怎么工作的? (How does dns work?)
其实这个问题每次看的时候都觉得很明白,但是很久之后就忘记了,所以这次准备记录下来.深入到这个过程的各个细节之中,以后多看看. Step 1 请求缓存信息: 当你在开始访问一个 www.baidu.co ...
- centso7 安装redmine
一.安装rvm ###安装rvm gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D3 ...
- 使用composer安装php的相关框架
使用composer来安装php的相关框架,不需要事先准备composer.json以及conmposer.lock以及composer.phar等文件: 直接在项目根目录下是使用composer r ...
- servletContext和request对象的生命周期比较
ServletContext: 创建:服务器启动 销毁:服务器关闭 域的作用范围:整个web应用 Request: 创建:访问时创建request 销毁:响应结束request销毁 域的作用范围:一次 ...
- php2
session //将用户的会话数据存储在服务端,通过 session_start()开启session,通过$_SESSION读写session session_start(); //开启ses ...
- Power Spectral Density
对于一个特定的信号来说,有时域与频域两个表达形式,时域表现的是信号随时间的变化,频域表现的是信号在不同频率上的分量.在信号处理中,通常会对信号进行傅里叶变换得到该信号的频域表示,从而得到信号在频域上的 ...
- Qt QLineEdit
//lineEdit显示文字 QLineEdit *lineEdit = new QLineEdit(widget); lineEdit->setObjectName(QString()); l ...
- CDQ题目套路总结 [未完成]
CDQ学习资料 day1cdq分治相关 CDQ的IOI论文 1.优化斜率dp 左边对右边影响维护一个凸包解决 需要知识:①凸包②斜率dp 题目:√ HDU3842 Machine Works HY ...
- python爬取豆瓣前25个影片内容的正则表达式练习
通过python正则表达式获取豆瓣top250的第一页的25个影片排名,影片名字,影片连接,导演,主演,上映日期,国家,剧情,评分,评价人数的内容 网页html内容: <ol class=&qu ...
- project 2013 删除资源
1.分析 在资源名称这边一旦输入过资源名称,下次点击下拉框就会出现历史记录,如何删除 2.步骤 资源-->分配资源-->点击资源名称,按F2,按DEL键