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 ...
随机推荐
- 1001.A+B Format(10)
1001.A+B Format(20) github链接:[example link](https://github.com/wgc12/object-oriented 1.对题目的理解: 首先这道题 ...
- Java的数组堆溢出问题
在写测试方法的时候,生成了一个数组,之后报了堆溢出错误,这样的报错一般来说只要有一些JVM的基础都知道要用-Xmx.-Xms来开更大的堆,接下来看看我碰到的一个堆溢出的问题 在测试代码中开了一个500 ...
- 将jar包添加到maven仓库
Maven资源库配置 访问http://mvnrepository.com/,在搜索栏中输入你要搜索的 JAR 包的关键字 例如下载ImpalaJDBC41这个jar包 选择你想要下载的Jar包版 ...
- 深入了解MyBatis二级缓存
深入了解MyBatis二级缓存 标签: mybatis二级缓存 2015-03-30 08:57 41446人阅读 评论(13) 收藏 举报 分类: Mybatis(51) 版权声明:版权归博主所 ...
- Maven 安装源码和文档到本地仓库
一: 1: mvn source:jar 生成源码的jar包 2: mvn source:jar install 将源码安装到本地仓库 ,可以直接mvn source:jar install 一部 ...
- python内置模块
time--时间模块 时间三大类: 时间戳 time.time() 结构化时间(年月日时分秒 一周内第几天,一年内第几天,是否夏令时) time.localtime() time.gmtime() ...
- Java 包(package)
为了更好地组织类,Java 提供了包机制,用于区别类名的命名空间. 1.包的作用 1.把功能相似或相关的类或接口组织在同一个包中,方便类的查找和使用. 2.如同文件夹一样,包也采用了树形目录的存储方式 ...
- Mac 下搭建服务器
1.开启服务器 Apache. sudo apachectl -k start 打开浏览器,在地址栏输入 localhost,如果出现 It works! 那么第一步已经成功了,如果没成功---出门左 ...
- Android-ProgressDialog点击对话框外部是不让其消失
1)ProgressDialog.setCanceledOnTouchOutside(false); 调用这个方法时,按对话框以外的地方不起作用.按返回键还起作用 2)ProgressDialog.s ...
- Android 给TextView中的字体加上“中间线”
大家都知道在做购物App或者购物网站的时候,商品价格往往会有一个“现价”和“原价”而原价往往会在中间加上一个黑色的横线.便于醒目客户,但是这种效果在App中应该怎样做呢? 废话不多少,直接给大家看代码 ...