1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties:
- The left subtree of a node contains only nodes with keys less than or equal to the node's key.
- The right subtree of a node contains only nodes with keys greater than the node's key.
- Both the left and right subtrees must also be binary search trees.
Insert a sequence of numbers into an initially empty binary search tree. Then you are supposed to count the total number of nodes in the lowest 2 levels of the resulting tree.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (<=1000) which is the size of the input sequence. Then given in the next line are the N integers in [-1000 1000] which are supposed to be inserted into an initially empty binary search tree.
Output Specification:
For each case, print in one line the numbers of nodes in the lowest 2 levels of the resulting tree in the format:
n1 + n2 = n
where n1 is the number of nodes in the lowest level, n2 is that of the level above, and n is the sum.
Sample Input:
9
25 30 42 16 20 20 35 -5 28
Sample Output:
2 + 4 = 6
建树,然后不断更新最大深度和次大深度,及相应点个数。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
struct tree
{
int data;
tree *left,*right;
tree()
{
data = ;
left = right = NULL;
}
}*head;
int n;
int m,c1,c2;
tree *insert_(tree *r,int d,int h)
{
if(r == NULL)
{
r = new tree();
r -> data = d;
if(h > m)
{
m = h;
c2 = c1;
c1 = ;
}
else if(h == m)c1 ++;
else if(h == m - )c2 ++;
}
else if(d > r -> data)
{
r -> right = insert_(r -> right,d,h + );
}
else
{
r -> left = insert_(r -> left,d,h + );
}
return r;
}
int main()
{
int d;
scanf("%d",&n);
head = NULL;
for(int i = ;i < n;i ++)
{
scanf("%d",&d);
head = insert_(head,d,);
}
printf("%d + %d = %d",c1,c2,c1 + c2);
}
1115. Counting Nodes in a BST (30)的更多相关文章
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
- 【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 分)
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
- 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 1115 Counting Nodes in a BST[构建BST]
1115 Counting Nodes in a BST(30 分) A Binary Search Tree (BST) is recursively defined as a binary tre ...
随机推荐
- Android Studio 中 Gradle 依赖的统一管理(rootProjectt)
最近遇到Android Studio 中 Gradle 依赖的统一管理的不懂得地方,看大神的也没看懂,百度了一下,使用起来还挺方便 下面是链接,在这里我就不详细说明了, http://www.jian ...
- 【BZOJ3316】JC loves Mkk 分数规划+单调队列
[BZOJ3316]JC loves Mkk Description Input 第1行,包含三个整数.n,L,R.第2行n个数,代表a[1..n]. Output 仅1行,表示询问答案.如果答案是整 ...
- android菜鸟学习笔记21----ContentProvider(一)ContentProvider的简单使用
ContentProvider是Android四大组件之一,它用来封装数据,并通过ContentResolver接口将数据提供给其他应用.只有当需要在多个应用之间共享数据时才会用到ContentPro ...
- linux环境下redis安装
本篇文章主要说明的是Linux环境下redis数据库的安装: 首先进入目标目录: 下载安装包,执行命令: wget http://download.redis.io/releases/redis-4. ...
- 在Nginx/Openresty中启用http2支持
转自个人博客 chinazt.cc 以下摘自http2的介绍: HTTP/2 源自 SPDY/2 SPDY 系列协议由谷歌开发,于 2009 年公开.它的设计目标是降低 50% 的页面加载时间.当下很 ...
- CentOS 7.0 systemd
CentOS 7 已经切换到 systemd,系统指令也有所变化.之前用于启动.重启.停止各种服务的service 作为向后兼容的指令还能使用,但是将来可能会消失.同时,chkconfig 也改成了s ...
- 探测web服务器质量——pycurl
pycurl是一个用C语言写的libcurl Python实现,功能非常强大,支持的操作协议有FTP.HTTP.HTTPS.TELNET等,可以理解为Linux下curl命令功能的Python封装,简 ...
- flask初次搭建rest服务笔记
官网中有用的记录一下,太多只是记录了最简单的官网docs:http://flask.pocoo.org/docs/0.12/ 跑起来一个程序 $ export FLASK_APP=hello.py $ ...
- ubuntu配置jdk环境
简单记录如下: 1. 下载JDK并解压后,复制到想要放置的目录,本文以“/usr/lib/jdk”为例: 2. 编辑配置文件,可以是“/etc/profile”或者“~/.bashrc”. 输入命令“ ...
- (转)Windows下面安装和配置MySQL(5.6.20)
原文地址:http://www.cnblogs.com/qiyebao/p/3887055.html 1.首先到http://dev.mysql.com/ 上下载windows版mysql5.6免安装 ...