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 ...
随机推荐
- ROS_RGB-D SLAM学习笔记--室内环境测试
ROS_RGB-D SLAM学习笔记 RTAB-Map's ros-pkg. RTAB-Map is a RGB-D SLAM approach with real-time constraints. ...
- 谈谈java虚拟机
本文可作为北京圣思元深入java虚拟机的课堂笔记. 先看一个令人dan teng的面试题 public class Singleton { public static Singleton s=new ...
- Hibernate统计表中的条数
/** * 判断积分日志表中是否有某个用户的注册日志 */@Transactional(propagation = Propagation.REQUIRED)public boolean isE ...
- 【面试笔试算法】Program 6: 字符消除(hiho题库)
时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi最近在玩一个字符消除游戏.给定一个只包含大写字母"ABC"的字符串s,消除过程是如下进行的: 1) ...
- SharePoint 2013 入门教程 [不断更新~]
以下文章是自己在学习SharePoint的过程中,不断积累和总结的博文,现在总结一个目录,分享给大家.这个博客也是自己从SharePoint入门,到一个SharePoint开发的成长记录,里面记录的都 ...
- PS 滤镜——素描算法(一)
这个算法结合高斯滤波和图层混合中的颜色减淡模式实现. 可以参考相关博客: http://blog.csdn.net/wsfdl/article/details/7610634 本文增加了一点调色,使得 ...
- 面试之路(29)-TCP流量控制和拥塞控制-滑动窗口协议详解
拥塞: 拥塞发生的主要原因在于网络能够提供的资源不足以满足用户的需求,这些资源包括缓存空间.链路带宽容量和中间节点的处理能力.由于互联网的设计机制导致其缺乏"接纳控制"能力,因此在 ...
- Android的源代码下载教程-android学习之旅(102)
一.环境准备 1.安装ubuntu系统,或者虚拟机. 2.安装java的sdk 3.安装依赖包 ,命令是:sudo apt-get install flex bison gperf libsdl-de ...
- Android开发学习总结(二)——使用Android Studio搭建Android集成开发环境
有很长一段时间没有更新博客了,最近实在是太忙了,没有时间去总结,现在终于可以有时间去总结一些Android上面的东西了,很久以前写过这篇关于使用Android Studio搭建Android集成开发环 ...
- spring boot之入门配置(一)
yml.properties配置文件 yml相比properties配置文件,yml可以省略不必要的前缀,并且看起来更加的有层次感.推荐使用yml文件. @Value 根据配置文件的配置项获取对应的v ...