PAT1115:Counting Nodes in a BST
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 思路 简单题,打印出二叉搜索树最后两层的节点数之和,格式为:"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的更多相关文章
- 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 ...
- 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 ...
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- PAT_A1115#Counting Nodes in a BST
Source: PAT A1115 Counting Nodes in a BST (30 分) Description: A Binary Search Tree (BST) is recursiv ...
- A1115. Counting Nodes in a BST
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 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 ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- 1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- of这个变态
英式口语还能听懂,一到美式,连读,爆破,就让人疯掉. 尤其big bang theory, of就是个变态,其读法有,英[əv, əv, v, f] 美[əv, ɑv,əv].但大部分都是/əv/. ...
- VS2005宏无法运行的问题(打了补丁MS14-009之后)
VS2005宏无法运行的问题(打了补丁MS14-009之后) 部门很多同事都是使用VS的宏来给源文件添加文件头,给函数.类添加注释等等,大概是14年2月份之后(根据lucifer提供的时间),这些宏突 ...
- 《java入门第一季》之面向对象(继承总结)
继承已经全部写完了,接下来通过几个案例对继承问题做一个总结: /* 看程序写结果: A:成员变量 就近原则 B:this和super的问题 this访问本类的成员 super访问父类的成员 C:子类构 ...
- Oracle Applications DBA 基础(二)
6.OAM及系统管理 2014年9月13日 20:40 参考资料: 1.Oracle Applications System Administrator's Guide - Configuration ...
- Unity Singleton 单例类(Unity3D开发之二十)
猴子原创,欢迎转载.转载请注明: 转载自Cocos2Der-CSDN,谢谢! 原文地址: http://blog.csdn.net/cocos2der/article/details/47335197 ...
- lpad函数
函数介绍 lpad函数是Oracle数据库函数,lpad函数从左边对字符串使用指定的字符进行填充.从其字面意思也可以理解,l是left的简写,pad是填充的意思,所以lpad就是从左边填充的意思. 2 ...
- day08_Servlet学习笔记
============================================================ 一.什么是Servlet?(非常非常重要) servlet 是运行在 Web ...
- java web--DOM
Dom总结dom:文档对象模型的简称.dom的解析:与XML一样遵循同样的规范 将标记型文档解析成一棵DOM树,并将树中的内容都封装成节点对象. 如果html文档过大,同样会造成解析过慢,怎么使用sa ...
- tomcat启动非常慢;连接oracle数据库失败,jdbc错误日志提示connection reset;测试主机间网络互通及数据库端口都正常
[判断确认:这时候大家可能要去检查一下/dev/random 这个设备档案.可以用cat /dev/random 来看它的内容,如果你发现他一直没显示任何内容﹝可能是乱码数字之类的﹞,那就是它出问 ...
- Nginx日志配置及配置调试
防火墙内的内网服务器,因为网关传过来的remot_addr都一样,不得不对Nginx的日志格式做了配置 配置语法如下: log_format myformat '$http_x_forwarded ...