PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum:
n1 + n2 = sum
递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两层的节点个数,也可以直接一遍dfs,顺便存储各个层的节点数。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#define LEFT 1
#define RIGHT 2
using namespace std;
/*
建立BST,两次dfs
一开始直接用index表示二叉树
即节点i的左孩子为2*i,右孩子为2*i+1
但是这样就会导致超出存储大小。。。
*/
const int maxn=+; //一开始设置成了1000导致段错误
int a[maxn];
int maxlayer=;
int n1=,n2=;
int cnt;
struct Node{
int left=-,right=-;
int id=-;
int value;
}node[maxn];
/*
root is the current node
val is the value of root
fa is the father node of root
LR tells that root is left or right child of fa
*/
void insertBST(int root,int val,int fa,int LR){
if(root==-){
cnt++;
node[cnt].value=val;
if(LR==LEFT){
node[fa].left=cnt;
}
else{
node[fa].right=cnt;
}
return;
}
if(val<=node[root].value){
insertBST(node[root].left,val,root,LEFT);
}
else{
insertBST(node[root].right,val,root,RIGHT);
}
}
void dfs(int root,int layer){
if(root==-){
maxlayer=max(maxlayer,layer-);
return;
}
dfs(node[root].left,layer+);
dfs(node[root].right,layer+);
} void dfsAns(int root,int layer){
if(root==-){
return;
}
if(layer==maxlayer){
n1++;
}
if(layer==maxlayer-){
n2++;
}
dfsAns(node[root].left,layer+);
dfsAns(node[root].right,layer+);
}
int main()
{
int n;
scanf("%d",&n);
int a;
memset(node,-,sizeof(node));
scanf("%d",&a);
node[].value=a;
cnt=;
for(int i=;i<n;i++){
scanf("%d",&a);
insertBST(,a,-,-);
}
dfs(,);
dfsAns(,);
printf("%d + %d = %d",n1,n2,n1+n2);
return ;
}
PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)的更多相关文章
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- PAT甲题题解-1004. Counting Leaves (30)-统计每层叶子节点个数+dfs
统计每层的叶子节点个数建树,然后dfs即可 #include <iostream> #include <cstdio> #include <algorithm> # ...
- PAT甲题题解-1049. Counting Ones-数学问题
n位数,总共有0~10^n-1共计10^n个数那么所有数出现的总次数变为n*(10^n)个数1出现的次数便是十分之一,所以n位数中,1出现的次数为n*10^(n-1)知道这一个后,接下来就方便求了. ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- PAT甲题题解1099. Build A Binary Search Tree (30)-二叉树遍历
题目就是给出一棵二叉搜索树,已知根节点为0,并且给出一个序列要插入到这课二叉树中,求这棵二叉树层次遍历后的序列. 用结构体建立节点,val表示该节点存储的值,left指向左孩子,right指向右孩子. ...
- 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 proper ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
- 【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 ...
随机推荐
- Beta阶段第一次冲刺
Beta阶段第一次冲刺 以后严格按照Git标准来,组员有上传Git的才有贡献分没有的为0 代码签入图 1.part1 -站立式会议照片 2.part2 -项目燃尽图 3.part3 -项目进展 1.正 ...
- 转 10 个 Nginx 的安全提示
Nginx是当今最流行的Web服务器之一.它为世界上7%的web流量提供服务而且正在以惊人的速度增长.它是个让人惊奇的服务器,我愿意部署它. 下面是一个常见安全陷阱和解决方案的列表,它可以辅助来确保你 ...
- 闲谈CDN网络架构
CDN也就是内容分布网络(Context Delivery Network),它是构筑在现有interent上的一种先进的流量分配网络.其目的是通过现有的Internet中增加一层新的网络架构,将网站 ...
- loli的搜索测试-5
今天早上听说不放假感觉很sad,又听说要考试感觉更sad了,早读的时候Juan_feng表示即使考也不会考虫食算,结果上午并没有考试,就自己做了几道题.不得不说做题的决定实在是太明智了,做NOIP历年 ...
- oracle 创建create user 及授权grant 查看登陆的用户
show user; select sys_context('userenv','session_user') from dual; select user from dual; 查看所有登录的用户必 ...
- 【转】 python中 * 的用法
转自:https://www.cnblogs.com/jony7/p/8035376.html 1.表示乘号 2.表示倍数,例如: def T(msg,time=1): print((msg+' ...
- OpenCV——图像的矩(计算矩、轮廓面积、轮廓或曲线长度)
图像矩描述了图像的全局特征 一阶矩与形状有关 二阶距显示曲线围绕直线平均值的扩展程度 三阶矩是关于平均值的对称性测量 由二阶和三阶矩可以导出7个不变矩,不变矩是图像的统计特性,满足平移.伸缩.旋转的不 ...
- OpenCV——图像金字塔和图片尺寸缩放
- 【Git】删除某个全局配置项
1.查看Git所有配置 git config --list 2.删除全局配置项 (1)终端执行命令: git config --global --unset user.name (2)编辑配置文件: ...
- Android 绘图时实现双缓冲
一.双缓冲技术原理: 在内存中创建一片内存区域,把将要绘制的图片预先绘制到内存中,在绘制显示的时候直接获取缓冲区的图片进行绘制.更具体一点来说:先通过setBitmap方法将要绘制的所有的图形绘制到一 ...