【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 ...
随机推荐
- 浏览器对象模型“BOM”,对浏览器窗口进行访问和操作
location对象 location.href url地址 location.hash 锚点 location.hostname 主机名(需要放到服务器上) location.ho ...
- Umi 小白纪实(一)—— 创建项目&常用配置
umi 是一个企业级 react 应用框架,也是蚂蚁金服的底层前端框架 <蚂蚁金服的前端框架和工程化实践> 一.安装脚手架 在创建项目之前,需要保证有 node 8.10 以上的环境 可以 ...
- CF #618 div.2
序 闲来无事,打场CF,本人蒟蒻,考场A了前三道,第四有解答 正文 T1 Non-zero 是道水题.... 给你一个序列a.要求你输出最少的操作次数使这个序列的累和与累乘都不为0: 一次操作指给\( ...
- Mayor's posters POJ - 2528 线段树区间覆盖
//线段树区间覆盖 #include<cstdio> #include<cstring> #include<iostream> #include<algori ...
- warning Attribute 'showExpand' must be hyphenated
报错翻译:警告属性“ showExpand”必须带连字符 报错原因父组件给子组件传参时,使用驼峰命名法,导致ESLint检测出语法问题,如下↓ 改成这样就ok了
- 报错:无法加载文件 D:\nodejs\node_global\webpack.ps1,因为在此系统上禁止运行脚本...
解决报错:(1)以管理员身份运行vs code (2)在终端执行:get-ExecutionPolicy,显示Restricted(表示状态是禁止的) [受限制的.保密的] (3)在终端执行:set ...
- 2018 IEEE极限编程大赛 题解
去年742,今年72,也算一种小小的进步. 明年前30(笑 1. Drawing Rooted Binary Trees 给定一个树的中序和前序的遍历,要求输出这棵树(包括空格的) #include ...
- 【Debian学徒记事】Debian快速呼出Terminal终端
Debian快速呼出Terminal终端 书接上回,Debian已经安装完毕 失踪的Ctrl+Alt+T 安装完毕启动,我发现了剑很诡异的事,Ctrl+Alt+T居然失灵了 (在多次测试后发现,Deb ...
- html无卡顿动画实现——requestAnimationFrame
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml"> ...
- Codeforces Round #592 (Div. 2) D - Paint the Tree
题目链接:https://codeforces.com/contest/1244/problem/D 题意:给你一个树,让你把树上的每个节点染成三种颜色,使得任意三个互相相邻的节点颜色都不一样(意思是 ...