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]的更多相关文章

  1. PAT 1115 Counting Nodes in a BST

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  2. 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 ...

  3. 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 ...

  4. [二叉查找树] 1115. Counting Nodes in a BST (30)

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  6. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

  7. 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 ...

  8. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

  9. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

随机推荐

  1. 去掉Scala的糖衣(4) -- Type Aliase

    我的新博客地址:http://cuipengfei.me/blog/2013/12/23/desugar-scala-4/ Scala中有一个type关键字,用来给类型或者是操作起别名,用起来很是方便 ...

  2. ubuntu14.04中安装jdk

    1. 下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 将下载的 .g ...

  3. Spring Cloud的子项目,大致可分成两类

    Spring Cloud的子项目,大致可分成两类,一类是对现有成熟框架”Spring Boot化”的封装和抽象,也是数量最多的项目:第二类是开发了一部分分布式系统的基础设施的实现,如Spring Cl ...

  4. hdu 1115:Lifting the Stone(计算几何,求多边形重心。 过年好!)

    Lifting the Stone Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  5. boost实用工具:typeof库 BOOST_TYPE BOOST_AUTO

    boost::typeof库中使用宏BOOST_TYPE和BOOST_AUTO来模拟C++11关键字typeof和auto  C++ Code  123456789101112131415161718 ...

  6. hdu 4739(状态压缩)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4739 思路:状态压缩. #include<iostream> #include<cs ...

  7. Python爬虫(七)

    源码: import requests import re from my_mysql import MysqlConnect # 获取详情页链接和电影名称 def get_urls(page): u ...

  8. js控制radio选中

    经常会遇到js控制radio选中和切换的问题 之前一直使用的是checked属性来完成的 但是现在发现这个属性有个大问题 今天就是用js给选中radio的赋值,使用的$().attr("ch ...

  9. ArcGIS ArcMap 问题(ArcMap闪退、cx_oracle安装不上)

    一.问题描述 1.ArcMap闪退 2.安装32位cx_oracle提示python目录不存在 二.解决方案 1.修改pythoncore的文件目录,指向C:\Python27\ArcGIS10.3\ ...

  10. hdu1244(dp)

    简单dp Max Sum Plus Plus Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...