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 ...
随机推荐
- Hexo搭建博客笔记
Hexo搭建(建议看ppt:https://files.cnblogs.com/files/-SANG/%E4%BD%A0%E7%9A%84%E7%8C%AB.pptx ) 安装Git https:/ ...
- redis 配置文件示例
# redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位,# 通常的格式就是 1k 5gb 4m 等酱紫:## 1k => 1000 bytes# 1kb =& ...
- olivettifaces数据集实现人脸识别代码
数据集: # -*- coding: utf-8 -*- """ Created on Wed Apr 24 18:21:21 2019 @author: 92958 & ...
- Bank项目
项目概述: 技术涵盖:由 8 组由浅入深的模块构成,应用如下技术:面向对象的封装性.构造器.引用类型的成员变量.异构数组.继承.多态.方法的重载.方法的重写.包装类.单子模式.异常.集合. 实现功能: ...
- Swift 并行编程现状和展望 - async/await 和参与者模式
这篇文章不是针对当前版本 Swift 3 的,而是对预计于 2018 年发布的 Swift 5 的一些特性的猜想.如果两年后我还记得这篇文章,可能会回来更新一波.在此之前,请当作一篇对现代语言并行编程 ...
- weex+vue2.x 踩坑实录(不定期更新)
执行 npm start 显示空白页面 这个是开始使用weex就出现的一个大坑,说实话对新手真的很不友好. 1.打开控制台显示:Cannot assign to read only property ...
- MetaMask/json-rpc-engine
https://github.com/MetaMask/json-rpc-engine RpcEngine——MetaMask/json-rpc-engine https://github.com/M ...
- WorldWind源码剖析系列:挂件类Widgets
WorldWindow用户定制控件类中所包含的的挂件类Widgets控件主要有如下图所示的派生类.它们的类图如下所示. 鉴于挂件类Widgets及其派生类,相对简单,基本上都是些利用DirectX3D ...
- 筛选法求N以内的所有素数
素数:一个数只能被1和它本身整除的数.2是最小的素数 #include <iostream> using namespace std; #define NUM 100 ]; int mai ...
- jsp el的内置对象
一.el内置对象(11个): pageScope (掌握) requestScope (掌握) applicationScope (掌握) sessionScope (掌握) param (了解) p ...