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 ...
随机推荐
- 关于http的两种上传方法
http传输数据GET和POST的两种方法: 1.Post传输数据时,不需要在URL中显示出来,而Get方法要在URL中显示. 2.get方式传递的参数可以在URL上看见,安全性不高,反之post安全 ...
- Redis数据类型及其操作
redis数据类型即操作 1. 字符串 set 设置字符串 格式: set key value 例子: set name kainhuck get 获取字符串的值 格式: get key 例子: ge ...
- tornado 获取 路径上的参数
https://www.cnblogs.com/quzq/p/10975766.html class JavaHandler(RequestHandler): #重写RequestHandler中in ...
- Linux 安装gcc g++
Linux版本: cat /proc/version Linux version 3.10.0-693.11.6.el7.x86_64 (mockbuild@x86-041.build.eng.bos ...
- Vulkan SDK 之 Graphics Pipeline
A graphics pipeline consists of shader stages, a pipeline layout, a render pass, and fixed-function ...
- 七十七、SAP中数据库操作之多表联合查询
一.我们看一下SFLIGHT表和SPFLI表,表结构如下 二.这2个表的数据如下 三.我们代码如下 四.多表联合查询结果如下
- servlet中调用注入spring管理的dao(转)
今天做大型仪器的的时候遇到的问题,转下为了以后能用 http://blog.csdn.net/jiyingying_up/article/details/44803585 我们用spring的依赖注入 ...
- CSU 1126 DFS前缀和
在一棵树上找影响最小的某个点,某个点的影响是等于其他点到他的距离*其他点的权值 的和 我一开始也找不到什么好的方法,只能想到每个点暴力去判断,但是这样肯定会超时(10^5个点),又有点想用类似前缀和, ...
- 实验吧-隐写术-男神一般都很低调很低调的!!(stegsolve->Image Combiner + DES加密)
先介绍一下DES加密:(也可参考https://blog.csdn.net/zz_Caleb/article/details/87016017,第14个) 1)对称加密,参考:对称加密和非对称加密 2 ...
- 【Python】【Django】登录用户-链接Mysql