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& ...
随机推荐
- Linux用ssh登陆出现“Too many authentication failures for root”
vim /etc/ssh/sshd_config 最后参数 UseDNS no AddressFamily inet PermitRootLogin yes SyslogFacility AUTHPR ...
- 求逆元 - HNU 13412 Cookie Counter
Cookie Counter Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13412&a ...
- MyBatis Generator 学习
根据数据库,自动生成 VO.XML或者DAO的工具. 同大多数工具(或者框架)一样,需要加载一个配置文件,然后根据配置文件中的内容连接数据库,访问其中的表内容,最后生成实体类以及MAPPER. 占位用 ...
- oracle oci 调用 1
http://blog.163.com/earth_of_fire/blog/static/1368943200791211622278/(总结) http://blog.163.com/earth_ ...
- js 版本号
在web项目开发过程中,我们经常会引用css.js文件,更新文件后常出现缓存问题(明明更改了代码,在浏览器上访问的时候却没有发生变化),这种情况我们通常采用以下两种解决方案: 1.手动清除浏览器缓存 ...
- django admin后台css样式丢失
尼玛 坑爹啊 怎么光秃秃的,跟人家的不一样啊 打开firebug 发现报错,找不到css 通过google找到原因,是因为admin所需的js ,css等静态文件虽然都在django的安装目录内,但是 ...
- 三角剖分算法(delaunay)
开篇 在做一个Low Poly的课题,而这种低多边形的成像效果在现在设计中越来越被喜欢,其中的低多边形都是由三角形组成的. 而如何自动生成这些看起来很特殊的三角形,就是本章要讨论的内容. 项目地址: ...
- 系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体
系统管理模块_岗位管理_实现CRUD功能的具体步骤并设计Role实体 1,设计实体/表 设计实体 --> JavaBean --> hbm.xml --> 建表 设计Role实体 p ...
- pybot/robot命令参数说明【dos下执行命令pybot.bat --help查看】
Robot Framework -- A generic test automation framework Version: 3.0 (Python 3.4.0 on win32) Usage: r ...
- vue-router scrollBehavior无效的问题
在使用vue做单页面应用开发时候 使用vue-router作为路由控制器 在使用过程中发现每个页面打开都在原来的位置 不能返回到页面顶部位置 ,然后查看api文档 滚动行为 发现如下代码: con ...