PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]
题目
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The lef 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 lef 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
题目分析
已知一棵二叉查找树的建树序列,求二叉查找树最后两层的结点数
解题思路
思路 01
- 建树(非递归)
- bfs深度优先遍历,记录每层结点数和最大层数
思路 02
- 建树(递归)
- dfs广度优先遍历,记录每层结点数和最大层数
Code
Code 01
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1000;
struct node {
int data;
int h=0;
node * left=NULL;
node * right=NULL;
node() {
}
node(int _data, int _h) {
data = _data;
h=_h;
}
};
node * root;
int h[maxn],lcn[maxn],max_h;
void insert(int n) {
if(root==NULL) {
root=new node(n,1);
return;
}
node * p=root;
while(p!=NULL) {
if(p->data<n) {
if(p->right==NULL){
p->right=new node(n,p->h+1);
return;
}
else p=p->right;
} else {
if(p->left==NULL){
p->left=new node(n,p->h+1);
return;
}
else p=p->left;
}
}
}
void bfs() {
queue <node *> q;
q.push(root);
while(!q.empty()) {
node * now = q.front();
q.pop();
max_h=max(max_h,now->h);
lcn[now->h]++;
if(now->left!=NULL)q.push(now->left);
if(now->right!=NULL)q.push(now->right);
}
}
int main(int argc,char * argv[]) {
int n,rn;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&rn);
insert(rn);
}
bfs();
//根节点高度为1,高度0作为哨兵
printf("%d + %d = %d", lcn[max_h], lcn[max_h-1], lcn[max_h]+lcn[max_h-1]);
return 0;
}
Code 02
#include <iostream>
#include <queue>
using namespace std;
const int maxn=1000;
struct node {
int data;
int h=0;
node * left=NULL;
node * right=NULL;
node() {
}
node(int _data, int _h) {
data = _data;
h=_h;
}
};
node * root;
int h[maxn],lcn[maxn],max_h;
void insert(node* &root, int data, int dep) {
if(root == NULL) { //到达空结点时,即为需要插入的位置
root = new node(data,dep);
root->data = data;
root->left = root->right = NULL; //此句不能漏
return;
}
if(data <= root->data) insert(root->left, data, root->h+1); //插在左子树
else insert(root->right, data, root->h+1); //插在右子树
}
void dfs(node * now, int h){
if(now==NULL){
return;
}
max_h=max(max_h,h);
lcn[h]++;
if(now->left!=NULL)dfs(now->left, h+1);
if(now->right!=NULL)dfs(now->right, h+1);
}
int main(int argc,char * argv[]) {
int n,rn;
scanf("%d",&n);
for(int i=0; i<n; i++) {
scanf("%d",&rn);
insert(root,rn, 1);
}
dfs(root,1);
//根节点高度为1,高度0作为哨兵
printf("%d + %d = %d", lcn[max_h], lcn[max_h-1], lcn[max_h]+lcn[max_h-1]);
return 0;
}
PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]的更多相关文章
- PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
- [二叉查找树] 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【dfs】
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...
- 1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 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 ...
随机推荐
- 基于Hadoop3.1.2集群的Hive3.1.2安装(有不少坑)
前置条件: 已经安装好了带有HDFS, MapReduce, Yarn 功能的 Hadoop集群 链接: ubuntu18.04.2 hadoop3.1.2+zookeeper3.5.5高可用完全分布 ...
- Docker-harbor-V1.3.0 ”私有仓库“搭建 Easy
准备: centos 7.0 Docker version 1.12.6 docker-compose version 1.19.0 1: updata-yum: 更新yum 源 ...
- 如何从Domino迁移到Exchange 2010
从Domino 6.x迁移到Exchange 2010利用了微软提供的工具:Microsoft Transporter Suite,该工具不支持从Domino 6.X直接迁移至Exchange 2 ...
- 干干净净的grep
用grep -rn "xxxx" ./ 搜索. 有时候出现大量的 错误信息 主要有 1.Is a directory 2.no such file or directory 前 ...
- 杭电oj3306:Another kind of Fibonacci(矩阵快速幂)
Another kind of Fibonacci 题目链接 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (J ...
- Spring-IOC(基于XML配置)
什么是IOC? IOC: Inversion of Control(控制反转). 控制反转:将对象的创建权反转给(交给)Spring. 在使用Spring框架之后,对象的实例不再由调用者来创建,而 是 ...
- 五十六、SAP中LVC表格的常用布局属性LVC_S_LAYO
一.LVC_S_LAYO为表格常用的布局属性,包括网格线,宽度自适应,隐藏主键等 二.我们来对比使用前和使用后的表格,这个原始布局风格的表格 三.这个是设置了相关属性的表格
- 056-for循环中continue的使用
<?php ; $i <= ; $i++) { //for循环输出数值 == ) //判断变量是否为3的整数倍 { continue;//跳过本次循环剩余语句 } echo "$ ...
- 4. 异步多级缓存架构+nginx数据本地化渲染
- 基础语法-其它流程控制语句break和continue
基础语法-其它流程控制语句break和continue 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.break语句 /** * break语句 * @author 尹正杰 * ...