PAT 1115 Counting Nodes in a BST
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 [−10001000] 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> //建立二叉树和dfs
using namespace std;
int maxdeep=0, a=0, b=0;
struct node{
int value;
node* left=NULL;
node* right=NULL;
node(int v):value(v), left(NULL), right(NULL){
}
};
node* insert(int v, node* root, int deep){
if(!root){
node* temp=new node(v);
root =temp;
maxdeep=(deep>maxdeep?deep:maxdeep);
}else{
if(v<=root->value)
root->left=insert(v, root->left, deep+1);
else
root->right=insert(v, root->right, deep+1);
}
return root;
}
void preorder(node* root, int deep){
if(root==NULL)
return;
if(deep==maxdeep-1)
a++;
else if(deep==maxdeep)
b++;
preorder(root->left, deep+1);
preorder(root->right, deep+1);
}
int main(){
int n, v;
cin>>n;
node* root=NULL;
for(int i=0; i<n; i++){
cin>>v;
root=insert(v, root, 1);
}
preorder(root, 1);
cout<<b<<" + "<<a<<" = "<<a+b<<endl;
return 0;
}
PAT 1115 Counting Nodes in a BST的更多相关文章
- 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甲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
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- 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 ...
- PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
- PAT (Advanced Level) 1115. Counting Nodes in a BST (30)
简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...
随机推荐
- 1695 windows 2013
1695 windows 2013 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 话说adam ...
- Ubuntu下用命令行快速打开各类型文件(转)
nautilus /media/pm/文档/book/system/必读nautilus /media/pm/文档/book/android/ndk 内核/framerwork/android wai ...
- Android指纹识别深入浅出分析到实战
指纹识别这个名词听起来并不陌生,但是实际开发过程中用得并不多.Google从Android6.0(api23)开始才提供标准指纹识别支持,并对外提供指纹识别相关的接口.本文除了能适配6.0及以上系统, ...
- 手势识别官方教程(7)识别缩放手势用ScaleGestureDetector和SimpleOnScaleGestureListener
1.Use Touch to Perform Scaling As discussed in Detecting Common Gestures, GestureDetector helps you ...
- SQL SERVER中存储过程IN 参数条件的使用!!!
正常的传递 @SendStationID='''1'',''2''' 是无效,改用 @SendStationID='1,2,3,003,002' 调用以下的存储过程可以实现in 查询效果 USE [ ...
- 我正在学英语是用learning english还是用studying english?
学一门语言用 learn. study 表示深入研究,一般指在大学里.如果大学里的专业是英语,就可以说 study English. 1. If you study hard, you will le ...
- SQL Server之纵表与横表互转
1,纵表转横表 纵表结构 Table_A: 转换后的结构: 纵表转横表的SQL示例: SELECT Name , SUM(CASE WHEN Course = N'语文' THEN G ...
- 给定的逗号分隔的数字字符串转换为Table
--将给定的逗号分隔的数字字符串转换为Table CREATE FUNCTION [dbo].[fu_Split](@strString nvarchar(4000)) RETURNS @Result ...
- [ SCOI 2005 ] 最大子矩阵
\(\\\) \(Description\) 给出一个\(N\times M\)的有权矩阵,选出其中\(K\)个互不重叠的子矩阵,使得这\(K\)个子矩阵的权值和最大. \(N\in [1,100]\ ...
- StackOverflowError&OutOfMemoryError区别
在Java虚拟机规范中,针对内存分配规定两种异常状况,即StackOverflowError和OutOfMemoryError. StackOverflowError:当线程请求的内存大小大于所配置的 ...