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. 构造 - Codeforces Round #319 (Div. 1)C. Points on Plane

    Points on Plane Problem's Link Mean: 在二维坐标中给定n个点,求一条哈密顿通路. analyse: 一开始忽略了“无需保证路径最短”这个条件,一直在套最短哈密顿通路 ...

  2. Linux 串口编程

    今天对应用层串口编程进行了验证.程序来源于以下参考链接,自己进行了一些注释和更改,记录于此. Tony Liu, 2016-6-17, Shenzhen 参考链接 https://www.ibm.co ...

  3. mysql -- 创建存储过程 往数据表中新增字段

    需求: 往某数据库的某个表中新增一个字段(若该字段已存在,则不做操作:若该字段不存在,则新增) 百度了n久,没有符合要求的例子,只有参考加自己琢磨,最终终于给弄出来了,以下是几个版本的更迭 第一版: ...

  4. [css]解决iframe在ios设备上无法滚动

    原因: safari的webkit内核特性 解决方案: 在iframe外包裹一层div并另外设置其css属性为如下: -webkit-overflow-scrolling:touch; overflo ...

  5. SQL命令优化(积累)

    与数据库交互的基本语言是sql,数据库每次解析和执行sql语句多需要执行很多步骤.以sql server为例,当数据库收到一条查询语句时,语法分析器会扫描sql语句并将其分成逻辑单元(如关键词.表达式 ...

  6. UILabel 行间距设置

    NSMutableAttributedString * attributedString1 = [[NSMutableAttributedString alloc] initWithString:te ...

  7. JSP小例子——实现用户登录小例子(不涉及DB操作)

    实现用户登录小例子用户名和密码都为"admin",登陆成功使用服务器内部转发到login_success.jsp页面,并且提示登陆成功的用户名.如果登陆失败则请求重定向到login ...

  8. MemSQL start[c]up Round 1.b

    二分查找题, 不知道用double的人,用LL果断错了... B. Stadium and Games time limit per test 1 second memory limit per te ...

  9. LeetCode 笔记系列七 Substring with Concatenation of All Words

    题目:You are given a string, S, and a list of words, L, that are all of the same length. Find all star ...

  10. 面试之一:CMS收集器整理

      CMS收集器整理 @white 基本说明: 目标:获取最短回收停顿时间 算法:标记-清除算法 线程:并发 步骤: 初始标记:(会STP) 标记 GC Roots 能直接关联到的对象,速度很快 并发 ...