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 ...
随机推荐
- SPI协议解析
1. SPI物理层 SPI通讯需要使用4条线:3条总线和1条片选 . SPI遵循主从模式,3条总线分别是SCK.MOSI和MISO,片选线为nSS(低电平有效),SPI协议适用于一主多从的工作场景: ...
- 一文详解scala泛型及类型限定
今天知识星球球友,微信问浪尖了一个spark源码阅读中的类型限定问题.这个在spark源码很多处出现,所以今天浪尖就整理一下scala类型限定的内容.希望对大家有帮助. scala类型参数要点 1. ...
- Gym - 101158C Distribution Center
题意:n个传送带,传送带i运送编号为i的物品,机器人可以负责把传送带i上的物品放到传送带i + 1上,也可以把传送带i + 1上的物品放到传送带i上,机器人分布在传送带上x轴的不同位置,问每个传送带最 ...
- POJ 1852:Ants
Ants Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 11754 Accepted: 5167 Description ...
- postman带上token对接口进行测试
根据验证码进行登陆,登录之后返回token.然后请求其他接口时在header头中带上token访问其他接口进行测试 账户信息输入验证码 登陆成功,看到返回的token 我们这个项目可以刷新一下toke ...
- 工具类css框架
@charset "UTF-8"; * { -webkit-box-sizing: border-box; box-sizing: border-box; ...
- windows中git输错密码后不能修改问题
坑位 当使用git做代码管理的时候,如果仓库地址地选用的是https,在初始拉取代码时,需要输入账号和密码,如果不小心输错了,后续一直会验证失败,无法再重新更正账号信息 Why 因为git为了不让你每 ...
- 控制台连接oracle11g报ORA-12560异常
oracle11g R2 64bit oracleClient 11.2 32bit PL/SQL Developer 11.0.2 32bit 今天发现了一个奇怪的现象,如图: 后来发现机器上既有s ...
- PGSQL基本操作语句
; --更新数据 ,,) ; --插入数据 ORDER BY app_name,flag asc/desc ; --查询数据并且排序 offset ; --查询起点0开始查询,返回5条数据 ORDER ...
- selenium2Library无法启动chrome
使用其他浏览器都没有影响,唯独chrome启动不起来,去掉IE-连接-局域网设置-自动检测设置就OK了