题目

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. Centos 8双网卡设置

    原理:不管开发板是通过直连.路由器还是交换机连接到PC机,最终都是接到PC的以太网网卡(对笔记本来说,一般存在两个网卡,一个WIFI网卡和以太网网卡):因此要实现PC机与虚拟机的互ping,必须把虚拟 ...

  2. Golang的运算符优先级实操案例

    Golang的运算符优先级实操案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.运算符优先级案例 运算符是用来在程序运行时执行数学或逻辑运算的,在Go语言中,一个表达式可以包 ...

  3. C语言拾遗——strtok

    C语言拾遗——strtok 今天刷PAT的时候用到了这个strtok函数,顺手就记录一下 strtok函数包含于头文件string.h 语法:char *strtok( char *str1, con ...

  4. 154. 寻找旋转排序数组中的最小值 II

    转跳点:--\(˙<>˙)/-- 原本打算大年三十十一起写完的,结果这篇拖到了年初一…… 这道题比刚刚那道,麻烦一点,因为有重复,所以我们需要考虑重复的情况,就是刚刚的两种情况变成了三种: ...

  5. CGridCtrl只点击规定行中的按钮才弹出对话框

    在头文件中添加: afx_msg void OnClick(NMHDR* pNMHDR, LRESULT* pResult); 添加映射:ON_NOTIFY(NM_CLICK, IDC_CUSTOM1 ...

  6. git上传流程

    转: 1.在github上创建项目 2.使用git clone https://github.com/xxxxxxx/xxxxx.git克隆到本地 3.编辑项目 4.git add . (将改动添加到 ...

  7. JS - 对话框

    1,<span onclick="return confirmAct();">执行操作</span> 2, function confirmAct(){ i ...

  8. 概率图模型之EM算法

    一.EM算法概述 EM算法(Expectation Maximization Algorithm,期望极大算法)是一种迭代算法,用于求解含有隐变量的概率模型参数的极大似然估计(MLE)或极大后验概率估 ...

  9. TensorFlow--交互式使用--tf.InteractiveSession()

    用tf.Session()创建会话时只有在会话中run某个张量才能得到这个张量的运算结果,而交互式环境下如命令行.IPython,想要执行一行就得到结果,这就需要用到tf.InteractiveSes ...

  10. Oracle Exadata 学习笔记之核心特性Part1

    近年来,国内众多厂商都有一体机的产品,不过更多都是围绕硬件本身的堆砌和优化,那么这些产品和Oracle一体机最大的区别在哪里呢?最近读了李亚的<Oracle Exadata技术详解>,系统 ...