【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
题意:
输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树。输出最底层和最底层上一层的结点个数之和,例如x+y=x+y。
AAAAAccepted code:
#define HAVE_STRUCT_TIMESPEC
#include<bits/stdc++.h>
using namespace std;
typedef struct Node{
int value;
int vis;
int level;
int visl,visr;
Node *lchild,*rchild;
}node;
int a[];
int num[];
void insert_(node *now,int x,int y){
if(now->vis==){
now->value=x;
now->vis=;
now->level=y;
++num[y];
return ;
}
if(x<=now->value){
if(now->visl==){
now->lchild=new node();
now->visl=;
}
insert_(now->lchild,x,+y);
}
else{
if(now->visr==){
now->rchild=new node();
now->visr=;
}
insert_(now->rchild,x,+y);
}
return ;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n;
cin>>n;
for(int i=;i<=n;++i)
cin>>a[i];
node *root=new node();
root->value=a[];
root->vis=;
root->level=;
++num[];
for(int i=;i<=n;++i)
insert_(root,a[i],);
int mx=;
for(int i=;i<n;++i)
if(num[i])
mx=i;
cout<<num[mx]<<" + "<<num[mx-]<<" = "<<num[mx]+num[mx-];
return ;
}
【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)的更多相关文章
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- 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 ...
- [二叉查找树] 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甲级——A1115 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 (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- 1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- 清北学堂—2020.1提高储备营—Day 4 afternoon(动态规划初步(一))
qbxt Day 4 afternoon --2020.1.20 济南 主讲:顾霆枫 目录一览 1.动态规划初步 2.记忆化搜索 3.递推式动态规划 4.记忆话搜索与递推式动态规划的转化 5.状态转移 ...
- Percona Xtrabackup 备份工具
生成备份 $ xtrabackup --backup --target-dir=/data/backups/ 注:--target-dir可以放在my.cnf配置文件中.如果指定的目录不存在,xtra ...
- 使用SMTP协议发送邮件
class Program { static void Main(string[] args) { ) { try { inputmodel obj = new inputmodel(args); S ...
- MY_0003:设置界面显示单位
1,设置单位
- 2-1.了解Pod对象
1.Pod参数定义 # 必填,版本号 apiVersion: string kind: Pod # 必填,元数据 metadata: # 必填,Pod对象的名称(命名规范需要符合RFC 1035规范) ...
- 与soul上的一个妹子聊天有感
写此篇的原因: 妹子说,我考上公务员了~,当时自己自己顿时哽咽了,不知道说什么,习惯性的说了句,恭喜恭喜啊.感受到妹子的欢喜与喜悦,我也没必要打扰她的兴致,她开心就好了嘛. 每个人的成就都是自己奋斗的 ...
- cc.fade.fade
用cc.fadeIn之前要先把setOpacity(0),笑哭,啊啊啊啊啊.因为这个东西卡了好久,啊啊啊
- 《NVM-Express-1_4-2019.06.10-Ratified》学习笔记(6.5-加-8.2-加-8.3)End-to-end_Data_Protection
6.5 End-to-end Protection Information 端到端保护信息 包含数据转存的命令可以使用端到端数据保护.在这些命令内部[位于Command Dword 12 的 [29: ...
- 服务起不来,查看ps axj 看服务是否为守护进程(TPGID 为-1)
在linux命令行中输入: ps axj 查看服务进程的 TPGID 字段的值是否为-1 ,为-1表示为守护进程 不为-1表示不是守护进程,服务启动不起来,或者启动起来后又被杀死了
- Wannafly Winter Camp 2020 Day 5H Geometry PTSD - 构造,计算几何
Solution 考虑如何计算点到平面的距离,我们可以用行列式算出锥形的体积和底面的面积,来得到距离 考虑构造一个等边三角形,这样基本是满足 \(\geq 1.7\) 的条件的 于是我们对它的坐标枚举 ...