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& ...
随机推荐
- AJAX的中文乱码问题
/***********本人原创,欢迎转载,转载请保留本人信息*************/作者:wallimn电邮:wallimn@sohu.com博客:http://blog.csdn.net/wa ...
- 容斥 + 组合数学 ---Codeforces Round #317 A. Lengthening Sticks
Lengthening Sticks Problem's Link: http://codeforces.com/contest/571/problem/A Mean: 给出a,b,c,l,要求a+x ...
- C语言 百炼成钢24
/* 题目60:从键盘中输入一个不超过40个字符的字符串, 再输入3个位数(每次删除一个字符),删除对应 位数的字符,然后输出删除指定字符后的字符串. 输入:hellokityManGood 3 6 ...
- PyQt的Layout的比例化分块。
一. QGridLayout: // 列比 第0列与第1列之比为 1:2 layout2p1 -> setColumnStretch(0, 1); layout2p1 -> setColu ...
- mysql_ado的demo
winform程序 http://pan.baidu.com/s/1nvxm5br
- 【mysql】windows7 安装 Mysql
From: http://jingyan.baidu.com/article/e52e3615a1128c40c70c5174.html 安装(解压) ZIP Archive版是免安装的.只要解压就行 ...
- Ubuntu 16.04 LTS 安装libvips出现”Package vips was not found in the pkg-config search path”
使用libvips来操作图像,libvips的部署参考一个Node.js工程:https://github.com/lovell/sharp 在MAC下安装很顺利,到Linux环境下(Ubuntu 1 ...
- WebApi 异常处理解决方案
1.继承ExceptionFilterAttribute类,重写OnException方法 public class WebApiExceptionFilterAttribute : Exceptio ...
- Spring_day04--课程安排_回顾SSH框架知识点_SSH框架整合思想
Spring_day04 上节内容回顾 今天内容介绍 回顾SSH框架知识点 Hibernate框架 Struts2框架 Spring框架 SSH框架整合思想 整合struts2和spring框架 Sp ...
- Linux命令之乐--md5sum
md5sum命令用于生成和校验文件的md5值.它会逐位对文件的内容进行校验,它常用于检查文件的完整性. 读取文件的MD5值 [root@new ~]# md5sum /usr/local/sbin/* ...