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 ( ...
随机推荐
- Linux基础学习笔记6-SHELL编程
编程基础 程序:指令+数据 程序编程风格: 过程式:以指令为中心,数据服务于指令 对象式:以数据为中心,指令服务于数据 shell程序:提供了编程能力,解释执行 编程基本概念: 顺序执行:循环执行:选 ...
- python爬虫之win7Mongod安装使用
1.下载地址:https://www.mongodb.com/download-center#community 下载完成以后下一步下一步安装. 安装路径 还需要建立一个数据库存储位置C:\mongo ...
- 创建安全客户端Socket
SocketFactory factory = SSLSocketFactory.getDefault(); Socket socket = factory.create("localhos ...
- python学习笔记(12)--程序设计方法学
计算思维: 逻辑思维:推演和演绎 实证思维:实验和验证,引力波->实验 计算思维:设计和构造,计算机为代表,汉诺塔递归. 计算思维特征 抽象和自动化,抽象问题的计算过程,利用计算机自动化求解. ...
- python学习笔记(6)--条件分支语句
if xxxx: coding if xxxx: coding else: coding if xxxx: coding elif xxx: coding …… else: coding 或者一种简洁 ...
- 谈谈对C#中反射的一些理解和认识(上)
今天就平常用到的非常多的反射这个技术来做一个总结,当然关于反射需要讲解的东西实在是太多的内容,在一片文章中想要讲解清楚是非常难的,本篇博客也是就自己本人对这些内容学习后的一个总结,当然包括看书和自己写 ...
- 老男孩python学习自修第八天【函数式编程】
1.可变参数,将传参自动汇总成列表 2.可变参数,将参数自动汇总成字典 实战如下: #!/usr/bin/env python # _*_ coding:UTF-8 _*_ def show(*arg ...
- IBM rational rose画时序图软件破解安装
上边这个链接是开头的安装步骤,照着链接中的步骤安装完之后,接下来看下边. 1.然后安装完成打开软件“IBM Rational License Keyadministrator”.出现下图:选中第二项“ ...
- 一、MyCat的搭建
一.什么是mycat 简单直接点就是,MyCat其实就是一个数据库的中间件!一般我们都是app直接到数据库!有了MyCat以后,就是app到MyCat然后再访问数据库. mycat是个中间件,它负责连 ...
- Xamarin + MvvmCross 安装 Part 1
前言 最近,由于工作需要,公司准备开发移动端APP.临近年底,公司不准备大面招人,由于公司一直基于.net平台进行开发,本人自告奋勇,准备先用xamarin做下移动开发.开始了在网上不停的google ...