PAT甲级——A1115 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 (≤) which is the size of the input sequence. Then given in the next line are the N integers in [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
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
struct Node
{
int v;
Node *l, *r;
Node(int a = -) :v(a), l(nullptr), r(nullptr) {}
};
Node* root = nullptr;
int n, level = -, a;
vector<int>res;
void creatTree(Node*& root, int x)
{
if (root == nullptr)
{
root = new Node(x);
return;
}
if (x <= root->v)
creatTree(root->l, x);
else
creatTree(root->r, x);
}
void BFS(Node* root)
{
queue<Node*>q;
q.push(root);
while (!q.empty())
{
int num = ;
queue<Node*>temp;
while (!q.empty())
{
Node* p = q.front();
q.pop();
num++;
if (p->l != nullptr)
temp.push(p->l);
if (p->r != nullptr)
temp.push(p->r);
}
q = temp;
res.push_back(num);
}
}
int main()
{ cin >> n;
while (n--)
{
cin >> a;
creatTree(root, a);
}
BFS(root);
cout << res[res.size() - ] << " + " << res[res.size() - ] << " = " << res[res.size() - ] + res[res.size() - ] << endl;
return ;
}
PAT甲级——A1115 Counting Nodes in a BST【30】的更多相关文章
- 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 ...
- [二叉查找树] 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【dfs】
1115 Counting Nodes in a BST (30 分) A Binary Search Tree (BST) is recursively defined as a binary tr ...
- PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...
- A1115. 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 (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
随机推荐
- vue app外卖(5) 使用swiper 进行图片轮播
1.查看swiper 文档 https://www.swiper.com.cn/usage/index.html 2. 下载 npm install --save swiper 3.在页面引入 imp ...
- 多台服务器-SSH免密登录设置
在4台服务器-SSH免密登录设置,如以下4台服务器 master1 node001 node002 node003 我想在master1对4台服务器进行拉取或者分发任务或者是集群服务器的批量操作,但是 ...
- 24. Java SE 、 Java EE 、JavaME 、 JavaWeb 直接的区别和联系
这个是在别人博客抄的,并不是本人撰写 Java是一门编程语言.Java分为三大版本,SE即标准版,包含了Java核心类库,主要用来开发桌面应用:EE即企业版,包含SE,又有扩展部分(Servlet,J ...
- 01二重退背包+组合数学——cf1111d
退背包进阶,还是挺难想的 /* dp1[k]表示取到体积k的方案数 dp2[i][j][k]表示左侧必选ij的情况下,取到体积k的方案数 dp2[i][j][k]=dp1[k]-左侧不选ij的方案数 ...
- NX11.0和VS2013 创建NXOpen 开发模版失败解决方案【转载】
转载自PLM之家论坛 NX11.0和VS2013 创建NXOpen 开发模版失败解决方案 首先我觉得这个可能是西门子疏忽,基本上每个大版本没有补丁前都有类似问题,下面来说说怎么解决吧.注意这里版本,N ...
- Python-爬虫-爬取知乎的标题和当页显示的文字
# coding:utf-8 import requests from bs4 import BeautifulSoup quesNumStr = str(input("请输入搜索关键字:& ...
- Git 学习(二)Git 基础
Git 基础 Git 在保存和对待各种信息的时候与其它版本控制系统如 SVN 等等有很大差异,尽管操作起来的命令形式非常相近,理解这些差异将有助于防止你使用中的困惑. Git 记录的是什么? 如果有使 ...
- Ehcache3.x学习(二)分层的选项
Ehcache支持分层缓存的概念. 当想缓存堆内存以外的空间时,会发生下面的事情: 1.将数据添加到缓存意味着必须序列化key和value. 2.从缓存中读取数据意味着可能必须反序列化key和valu ...
- IOS中input光标跑偏问题的解决方法
ios端兼容input光标高度处理 在最近的项目中遇到一个问题,input输入框光标,在安卓手机上显示没有问题,但是在苹果手机上 当点击输入的时候,光标的高度和父盒子的高度一样.造成的原因就是给父盒子 ...
- C#中ORM的简单实现
ORM在功能上主要有两个: 把从数据库中查询返回的DataSet,DataTable转化为我们可以方便使用的实体类集合: 把要对数据库操作的实体类集合或条件转化为数据库可以直接执行的SQL语句.