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)的更多相关文章

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

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

  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甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

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

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

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

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

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

  6. 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)

    题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...

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

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

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

随机推荐

  1. window下安装php7的memcache扩展

    安装memcache:http://www.runoob.com/memcached/memcached-connection.html1.4.4 c:\memcached\memcached.exe ...

  2. Linux下的目录操作

    . 此层目录 .. 上层目录 - 前一个工作目录 ~ 当前用户的工作目录 ~account 表示account的家目录 1.cd:改变目录,change Directory的缩写. 2.pwd:显示当 ...

  3. WPF 支持集合绑定的控件

    WPF 支持集合绑定的控件 ListBox ComboBox ListView DataGrid

  4. EasyDSS直播服务器如何帮助用户解决OBS不能同时同步输出多路直播流到直播平台、CDN平台的限制

    最近有用户突然寻求帮助,大概的意思就是说: 他需要同步将桌面的直播同时RTMP发布到:斗鱼.熊猫TV等等多个平台,但是OBS又只能同时采集并发布推流直播到单一个平台,而且有时候在4G或者网络比较差的情 ...

  5. vue router-link子级返回父级页面

    vue-router嵌套路由,从二级路由跳转到一级路由时,间歇性导致一级路由重叠 解决方法: 1.使用this.$router.push跳转

  6. PHP自定义函数: 下载远程文件

    function httpcopy($url, $file="", $timeout=60) { $file = empty($file) ? pathinfo($url,PATH ...

  7. mysql语句, 空的 order by , 空的 where

    SQL语句里, 空的 where, where 1 AND status=1 空的 order by, order by null, updatetime desc 这种空值的情况, 是很有用处的: ...

  8. LDA中的先验知识

    LDA涉及到的先验知识有:二项分布.Gamma函数.Beta分布.多项分布.Dirichlet分布.马尔科夫链.MCMC.Gibbs Sampling.EM算法等. 二项分布 二项分布是N重伯努利分布 ...

  9. ALV 红绿灯编写

    [转http://lz357502668.blog.163.com/blog/static/164967432012417102133216/]ALV 指示燈 編寫 1,在內表中申請一個字段. 該字段 ...

  10. (转)Web Service和WCF的到底有什么区别

    [1]Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XM ...