题目

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

  1. 建树(非递归)
  2. bfs深度优先遍历,记录每层结点数和最大层数

思路 02

  1. 建树(递归)
  2. 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]的更多相关文章

  1. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  2. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

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

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

  4. 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 ...

  5. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  6. PAT 甲级 1115 Counting Nodes in a BST

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

  7. 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)

    题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...

  8. 1115. Counting Nodes in a BST (30)

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

  9. 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 ...

随机推荐

  1. mysql DDL语言

  2. SpringBoot-配置Java方式

    SpringBoot中使用Java方式配置步骤如下: 在类上加入@Configuration注解,代表作为配置类 在该类方法上加入@Bean注解,代表将方法返回的Bean加入Spring容器 在该类中 ...

  3. 字符设备驱动之LED驱动

    实现 ①编写驱动框架 ②编写硬件实现代码 (在Linux系统下操作硬件,需要操作虚拟地址,因此需要先把物理地址转换为虚拟地址 ioremap()) 如何实现单个灯的操作: 实现方法之一--操作次设备号 ...

  4. thinkphp配置到二级目录,不配置到根目录,访问除首页的其他路径都是404报错

    1.在nginx的配置里面,进行重定向 vi /etc/nginx/conf.d/default.conf 2.进入编辑 location /thinkphp/public { if (!-e $re ...

  5. 《ES6标准入门》(阮一峰)--9.数组的扩展

    1.扩展运算符 含义 扩展运算符(spread)是三个点(...).它好比 rest 参数的逆运算,将一个数组转为用逗号分隔的参数序列. console.log(...[1, 2, 3]) // 1 ...

  6. Xshell远程连接kali,SSH服务拒绝了密码

    在kali里面/etc/ssh/目录下,修改sshd_config文件,不是ssh_config,ssh_config是针对客户端的配置文件,而sshd_config是针对服务器端的配置文件. 找到# ...

  7. 电脑必须用U盘引导盘才能进系统解决办法

    昨天为了装Ubuntu双系统把系统给装崩了,结果重装win7系统之后出现了以下问题,百度的结果有些杂乱,解决过程自己做一下记录. 问题一:安装程序结束后,出现“Windows安装程序无法将Window ...

  8. 51nod 1055:最长等差数列

    1055 最长等差数列 基准时间限制:2 秒 空间限制:262144 KB 分值: 80 难度:5级算法题  收藏  取消关注 N个不同的正整数,找出由这些数组成的最长的等差数列. 例如:1 3 5 ...

  9. R函数

    1. sd() 求一组数据的标准差 > x = rep(1,15) > x [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 > sd(x) [1] 0 2.var ...

  10. css快速浏览

    meta <meta charset="utf-8" /> <meta name="keywords" content="key1, ...