1115. Counting Nodes in a BST (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

思路

简单题,打印出二叉搜索树最后两层的节点数之和,格式为:"n1 + n2 = sum(n1,n2)"。

1.根据输入建立二叉搜索树
2.前序遍历二叉树,用一个数组统计每一层的节点数,levels[i]代表第i层的节点数,用一个maxlevel不断更新最大层数。
3.输出levels[maxlevel] + levels[maxlevel - 1]。 代码
#include<iostream>
#include<vector>
using namespace std;
vector<int> levels;
int maxlevel = -1;
class node
{
public:
node(int _val):val(_val),left(nullptr),right(nullptr)
{
}
int val;
node* left;
node* right;
}; typedef node* treenode; void buildBST(treenode& root,int val)
{
if(val <= root->val && !root->left)
{
root->left = new node(val);
return;
}
if(val <= root->val && root->left)
{
buildBST(root->left,val);
return;
}
if(val > root->val && !root->right)
{
root->right = new node(val);
return;
}
if(val > root->val && root->right)
{
buildBST(root->right,val);
return;
}
} void countNodes(const treenode root,int level)
{
levels[level]++;
if(level > maxlevel)
maxlevel = level;
if(!root->left && !root->right)
return;
if(root->left)
countNodes(root->left,level + 1);
if(root->right)
countNodes(root->right,level + 1);
} int main()
{
int n;
while(cin >> n)
{
levels.resize(n + 1);
treenode root = new node(0);
cin >> root->val;
for(int i = 1;i < n;i++)
{
int tmp;
cin >> tmp;
buildBST(root,tmp);
}
countNodes(root,1);
cout << levels[maxlevel] << " + " << levels[maxlevel - 1] << " = " << levels[maxlevel] + levels[maxlevel - 1] << endl;
}
}

  

 

PAT1115:Counting Nodes in a BST的更多相关文章

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

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

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

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

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

  5. PAT_A1115#Counting Nodes in a BST

    Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursiv ...

  6. A1115. Counting Nodes in a BST

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

  7. PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs

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

  8. PAT 甲级 1115 Counting Nodes in a BST

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

  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. 自己动手写web框架----1

    本文可作为<<自己动手写struts–构建基于MVC的Web开发框架>>一书的读书笔记. 一个符合Model 2规范的web框架的架构图应该如下: Controller层的Se ...

  2. 【Qt编程】基于Qt的词典开发系列<三>--开始菜单的设计

    这篇文章讲讲如何实现开始菜单(或者称为主菜单)的设计.什么是开始菜单呢?我们拿常用的软件来用图例说明,大多数软件的开始菜单在左下角,如下图: 1.window 7的开始菜单 2.有道词典的主菜单 3. ...

  3. 关于SMALI语法

    dalvik字节码有两种类型,原始类型和引用类型.对象和数组是引用类型,其它都是原始类型.V  void,只能用于返回值类型Z  booleanB  byteS  shortC  charI  int ...

  4. OpenCV——素描

    具体的算法原理可以参考: PS滤镜,素描算法 // define head function #ifndef PS_ALGORITHM_H_INCLUDED #define PS_ALGORITHM_ ...

  5. objc:NSDateFormatter使用备忘

    NSDateFormatter类的实例可以将字符串的日期表示转换为NSDate对象或者反向转换. 如果只要显示日期不需要时间,则可以用-setDateStyle方法来设置显示日期的格式,有以下几种: ...

  6. MongoDB下载安装测试及使用

    1.下载安装 64位:mongodb-win32-x86_64-enterprise-windows-64-2.6.4-signed.msi 余数为1的 db.collection.find({ &q ...

  7. 开源组件photoView学习

    功能特性 支持放缩超出边界,多点触控和双击事件 滚动和滑动 和ViewPager等能完美兼容 矩阵变化等有回调,方便前台其他展示的改变 单击,长按都有回调提醒 源码剖析 那么怎么来学习他的源码呢,我们 ...

  8. 64位linux下安装ps模拟器ePSxe

    早就想在爱机上玩ps游戏,特别是彩京的1945一代和非常经典的实况足球2002版.在ubuntu64位下可以通过wine模拟的方式运行windows版的ePSxe,但是总觉得差些呢?非原生啊!网上搜了 ...

  9. obj-c编程02:给类自动合成存取方法

    我们在此篇对obj-c编程01中的Box的例子稍加改动,一是添加的自动合成存取器,二是将Box按照其标准的写法分成3个文件,即头文件Box.h,类实现文件Box.m,以及主文件test.m. 1.Bo ...

  10. MOOS通配符订阅

    MOOS通配符订阅 简介 通配符订阅是MOOSV10的重要进步,客户端可以通过此方式订阅名字和来源符合简单正则表达式的数据. 现在仅支持"*"和"?"两种通配符 ...