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. Cocos2D的平台检查宏

    为了避免在非iOS平台包含UIKit.h文件,需要在Prefix.pch文件中添加一个条件判断: #if __CC_PLATFORM_IOS #import <UIKit/UIKit.h> ...

  2. Leetcode_137_Single Number II

    本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/42877129 Given an array of inte ...

  3. Java集合之Hashtable

    和HashMap一样,Hashtable也是一个散列表,存储的内容也是键值对key-value映射.它继承了Dictionary,并实现了Map.Cloneable.io.Serializable接口 ...

  4. 线性表链式存储设计与实现 - API实现

    基本概念 链式存储定义 为了表示每个数据元素与其直接后继元素之间的逻辑关系,每个元素除了存储本身的信息外,还需要存储指示其直接后继的信息. 表头结点 链表中的第一个结点,包含指向第一个数据元素的指针以 ...

  5. 2016/1/9:深度剖析安卓Framebuffer设备驱动

    忙了几天,今天在公司居然没什么活干 ,所以早上就用公司的电脑写写之前在公司编写framebuffer的使用心得体会总结,这也算是一点开发经验,不过我还没写全,精华部分还是自己藏着吧.直到下午才开始有点 ...

  6. Gradle 1.12用户指南翻译——第二十八章. Jetty 插件

    其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Github上的地址: https://g ...

  7. DB Query Analyzer 6.03, the most excellent Universal DB Access tools on any Microsoft Windows OS

      DB Query Analyzer 6.03, the most excellent Universal database Access tools on any Microsoft Wind ...

  8. How Tomcat Works读书笔记三-------连接器

    几个概念 HttpServlet,Servlet Servlet是一个接口,定义了一种网络服务,我们所有的servlet都要实现它(或它的子类) HttpServlet是一个抽象类,它针对的就是htt ...

  9. myBatis源码学习之SqlSessionFactoryBuilder

    SqlSessionFactoryBuilder通过类名就可以看出这个类的主要作用就是创建一个SqlSessionFactory,通过输入mybatis配置文件的字节流或者字符流,生成XMLConfi ...

  10. unix命令自我总结

    三种参数类型 1⃣时间日期: cal times time 2⃣文字处理: ctl+v 输入控制字符 ${#str} str字符串长度 expr length $abc 同上 typeset -i x ...