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 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
题目大意:根据输入构建一棵二叉搜索树,并且计算最底层的两层共有多少个节点;n1是最底层的,n2是倒数第二层的。
//做的时候就有一个问题,如果这个二叉树只有一个根节点那怎么办呢?那么n2就是0了吗?
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std; struct Node{
int data,level;
Node* left;
Node *right;
};
int last=;
int no=,no2=;
void build(Node * &root,int data){
if(root==NULL){
root=new Node();
root->data=data;
root->left=NULL;
root->right=NULL;
return ;
}
if(data<root->data)build(root->left,data);
else build(root->right,data);
}
void dfs(Node *root,int level){
if(level>last)last=level;
root->level=level;
if(root->left!=NULL)dfs(root->left,level+);
if(root->right!=NULL) dfs(root->right,level+);
} void dfs2(Node* root){
if(root->level==last)no++;
else if(root->level==last-)no2++;
if(root->left!=NULL)dfs2(root->left);
if(root->right!=NULL) dfs2(root->right);
}
int main() {
int n;
cin>>n;
int temp;
Node* root=NULL;
for(int i=;i<n;i++){
cin>>temp;
//cout<<"oooo";
build(root,temp);
}
//if(root==NULL)cout<<"jjj";
dfs(root,);
dfs2(root);
cout<<no<<" + "<<no2<<" = "<<no+no2;
return ;
}
//第一次提交的时候这样,0,2,6三个测试点都没过,都是答案错误,只得了8分。。
#include <iostream>
#include <vector>
#include <cstdio>
using namespace std; struct Node{
int data,level;
Node* left;
Node *right;
};
int last=;
int no=,no2=;
void build(Node * &root,int data){
if(root==NULL){
root=new Node();
root->data=data;
root->left=NULL;
root->right=NULL;
return ;
}
if(data<=root->data)build(root->left,data);
else build(root->right,data);
}
void dfs(Node *root,int level){
if(level>last)last=level;
root->level=level;
if(root->left!=NULL)dfs(root->left,level+);
if(root->right!=NULL) dfs(root->right,level+);
} void dfs2(Node* root){
if(root->level==last)no++;
else if(root->level==last-)no2++;
if(root->left!=NULL)dfs2(root->left);
if(root->right!=NULL) dfs2(root->right);
}
int main() {
int n;
cin>>n;
int temp;
Node* root=NULL;
for(int i=;i<n;i++){
cin>>temp;
//cout<<"oooo";
build(root,temp);
}
//if(root==NULL)cout<<"jjj";
dfs(root,);
dfs2(root);
cout<<no<<" + "<<no2<<" = "<<no+no2;
return ;
}
//查看了被人的题解之后发现是因为,我弄错了BST的定义。
小于等于当前节点的都放到左子树!
PAT 1115 Counting Nodes in a BST[构建BST]的更多相关文章
- PAT 1115 Counting Nodes in a BST
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 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 (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- 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 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& ...
随机推荐
- 新手入门贴:史上最全Web端即时通讯技术原理详解
关于IM(InstantMessaging)即时通信类软件(如微信,QQ),大多数都是桌面应用程序或者native应用较为流行,而网上关于原生IM或桌面IM软件类的通信原理介绍也较多,此处不再赘述.而 ...
- BI开发之——Mdx基础语法(2)(转至指尖流淌)
结合webcast中老师的讲解,现在把基础语法应用通过几个案例应用如下: 一.维度的概念 上图中一个维度(Dimension):Region 改为度下有四个级别(Levels):country.pro ...
- trim() 是什么意思?
这是一个很常见的函数,他的所用是去掉字符序列左边和右边的空格,如字符串str = " ai lafu yo ";str = trim(str); cout << str ...
- socket udp广播和多播的简单实现
UDP广播与多播 作者:legend QQ:1327706646 使用UDP协议进行信息的传输之前不需要建议连接.换句话说就是客户端向服务器发送信息,客户端只需要给出服务器的ip地址和端口号,然后将信 ...
- 【BZOJ】1697: [Usaco2007 Feb]Cow Sorting牛排序(置换群)
http://www.lydsy.com/JudgeOnline/problem.php?id=1697 置换群T_T_T_T_T_T_T 很久以前在黑书和白书都看过,,,但是看不懂... 然后找了本 ...
- 邂逅明下(巴什博弈+hdu2897)
H - 邂逅明下 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- BT下载会损害硬盘吗
简而言之,这个问题是否存在,取决于网络带宽的发展速度与硬件性能的发展速度.如果硬件发展的速度快, 网络带宽速度发展慢,那么对大多数人而言,当前的硬件在慢速的带宽下载BT不会造成任何的硬盘损坏. ...
- Boatloader的工作流程
(1)第一节阶段的功能 1.硬件设备的初始化 2.载入u-boot第二阶段的代码到我们的RAM空间 3.设置好栈 4.跳转到第二阶段的代码入口 (2)第二阶段的功能 1.初始化本阶段所使用的硬件设备 ...
- Unable to acquire Oracle environment handle 问题的解决
---恢复内容开始--- 转自:http://blog.csdn.net/zhangweiwindow/article/details/6575224 今天重装了一下系统,所以以前工作时配置的pyth ...
- 用MCI处置WAV视频时,怎样才能让视频在当前窗口播放
用MCI处理WAV视频时,怎样才能让视频在当前窗口播放MCI播放视频默认是新开一个窗口播放,播放完毕返回原来的窗口,想着原来窗口播放如何做? mciSendCommand或mciSendString怎 ...