题意:

输入一个正整数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分)(二叉查找树)的更多相关文章

  1. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

  2. 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 ...

  3. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

  4. [二叉查找树] 1115. Counting Nodes in a BST (30)

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. 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 ...

  6. 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 ...

  7. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  8. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

  9. 1115. Counting Nodes in a BST (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. 关于MY Sql 查询锁表信息和解锁表

    1.查询锁住表信息 show OPEN TABLES where In_use > 0; 2.查看进程  show processlist; 3.解开锁住的表 需要杀掉锁住表的相关进程Id. k ...

  2. C++关于锁的总结(一)

    C++关于锁的总结(一) 线程中的锁分为两种,互斥锁和共享锁. 相关的头文件有<mutex>,<shared_mutex>,前者具有std::unique_lock操作,用于实 ...

  3. python3-cookbook笔记:第六章 数据编码和处理

    python3-cookbook中每个小节以问题.解决方案和讨论三个部分探讨了Python3在某类问题中的最优解决方式,或者说是探讨Python3本身的数据结构.函数.类等特性在某类问题上如何更好地使 ...

  4. XSS攻击解决办法 Spring mvc databinder

    XSS攻击解决办法 一.SpringMVC架构下@InitBinder方法 Controller方法的参数类型可以是基本类型,也可以是封装后的普通Java类型.若这个普通Java类型没有声明任何注解, ...

  5. MySQL系列(一):谈谈MySQL架构

    MySQL整体架构 与所有服务端软件一样,MySQL采用的也是C/S架构,即客户端(Client)与服务端(Server)架构,我们在使用MySQL的时候,都是以客户端的身份,发送请求连接到运行服务端 ...

  6. MySQL 8 模式匹配

    MySQL 提供了两种模式匹配: 标准 SQL 模式匹配 基于扩展正则的模式匹配 SQL 模式匹配使用 LIKE 命令进行模式匹配操作, _ 匹配任意单个字符.% 匹配任意多个字符,包含 0 个字符 ...

  7. 基于光盘配置yum源

    #开启自动挂载服务 systemctl start autofs #设置开机自动挂载 systemctl enable autofs #光盘自动挂载路径/misc/cd       “包含repoda ...

  8. mysql引擎介绍

    mysql存储引擎介绍: 插拔式的插件方式 存储引擎是指定在表上的,即一个库中的每一个表都可以指定专用的存储引擎 不管采用什么样的存储引擎,都会在数据区产生对应的一个frm文件(表结构定义描述文件) ...

  9. 在csv表格中修改/追加某行数据

    思路: 文本文件不能随意穿插信息,但是通过使用Seek()方法,可以在读取文本文件中移动光标从而修改所要修改的行. 思路步骤: 1.读取文件,打开csv文件,获取文件流,seek移动光标到开始, fo ...

  10. 堆排序用JavaScript实现

    class Heap { constructor (data) { this.data = data } sort () { let iArr = this.data let n = iArr.len ...