题意:

输入一个正整数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. 安装 mysqlclient 报 mysql_config not found

    安装 mysqlclient 报 mysql_config not found raise EnvironmentError("%s not found" % (mysql_con ...

  2. 图片-定义select向下箭头样式

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 有多少人在面试时,被Java 如何线程间通讯,问哭了?

    正常情况下,每个子线程完成各自的任务就可以结束了.不过有的时候,我们希望多个线程协同工作来完成某个任务,这时就涉及到了线程间通信了. 本文涉及到的知识点: thread.join(), object. ...

  4. 分布式任务调度XXL-JOB初体验

    简介 XXL-JOB是一个轻量级分布式任务调度平台,其核心设计目标是开发迅速.学习简单.轻量级.易扩展.现已开放源代码并接入多家公司线上产品线,开箱即用. 官方文档很完善,不多赘述.本文主要是搭建XX ...

  5. 剑指offer-面试题40-最下的k个数-快速排序

    /* 题目: 输入n个整数,找出其中最小的K个数.例如输入4,5,1,6,2,7,3,8这8个数字,则最小的4个数字是1,2,3,4,. */ /* 思路: 快速排序,找到第k+1大的数,其左边则为最 ...

  6. 「JOI 2017 Final」绳

    题意 loj 做法 首先我们观察到最后能折起来的充要条件是: 只有两个颜色,除首尾外,所有颜色块内的数量为偶数 因为为偶数,我们进一步推论: 所有颜色块起始位置奇偶性相同 然后因为增与减都会有相同花费 ...

  7. Vulnerable Kerbals CodeForces - 772C【拓展欧几里得建图+DAG上求最长路】

    根据拓展欧几里得对于同余方程 $ax+by=c$ ,有解的条件是 $(a,b)|c$. 那么对于构造的序列的数,前一个数 $a$  和后一个数 $b$ ,应该满足 $a*x=b(mod m)$ 即 $ ...

  8. java课后作业10.14

    一.简单总结一下java中类的初始化的规律: 1.类的构造函数优先 2.初始化块儿和定义时候的赋值,这俩个初始化方法优先级是一样的,在程序中谁最后执行,那么程序就采用谁的初始化值. 二.在java中想 ...

  9. take it away `electron-builder` Electron打包失败的问题

    出现这个问题 出现问题原因是程序占用 , 在后台进程中将Electron的服务干掉以后再打包

  10. 有意思的GitHub项目

    1.强大而又优雅的运维一键自动化管理工具,朋友公司在做,大牛支持,墙裂推荐 官方网站:http://www.bigops.com/ 2.用 PHP 写一个命令行音乐搜索下载器,支持 QQ.网易.酷狗. ...