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计算出最后两 ...
随机推荐
- visual studio snippet备忘
一.C++ classheadercpp.snippet <?xml version="1.0" encoding="utf-8"?> <Co ...
- 6367. 【NOIP2019模拟2019.9.25】工厂
题目 题目大意 给你一堆区间,将这些区间分成特定的几个集合,使得每个集合中的所有区间的并不为空. 求最大的每组区间的交的长度之和. 思考历程 一开始就认为这绝对是\(DP\)-- 试着找一些性质,结果 ...
- react使用阿里爸爸的iconfont时,不展示的问题
选择使用Unicode时: 正常使用如下,显示也是正常: <i className="iconfont"></i> 使用map去循环时,需将原本的,改成 ...
- delphi xe10 麦克风、摄像头操作
TakePhotoFromCameraAction1: TTakePhotoFromCameraAction; // 通过手机摄像头获取图片TakePhotoFromLibraryAction1: T ...
- Dart编程运算符
表达式是一种特殊类型的语句,它计算为一个值.每个表达都由 操作数 - 表示数据 运算符 - 定义如何处理操作数以生成值. 考虑以下表达式 2 + 3.在该表达式中,2和3是操作数,符号+(加号)是 运 ...
- 《DNS攻击防范科普系列1》—你的DNS服务器真的安全么?
DNS服务器,即域名服务器,它作为域名和IP地址之间的桥梁,在互联网访问中,起到至关重要的作用.每一个互联网上的域名,背后都至少有一个对应的DNS.对于一个企业来说,如果你的DNS服务器因为攻击而无法 ...
- thinkphp 错误调试
如果需要我们可以使用E方法输出错误信息并中断执行,例如: //输出错误信息,并中止执行 E($msg); 原3.1版本中的halt方法已经废弃,请使用E函数代替.
- 莫比乌斯反演+二维前缀和——hdu4746二刷
第二次做这题,求前缀和的时候还是卡住了 fg函数的反演是可以直接用莫比乌斯基本代换式来代换的 #include<bits/stdc++.h> using namespace std; #d ...
- Django使用步骤
pip install django django-admin startproject mysite tree django-admin startapp mysite_user django-ad ...
- storm集群的安装
storm图解 storm的基本概念 Topologies:拓扑,也俗称一个任务 Spoults:拓扑的消息源 Bolts:拓扑的处理逻辑单元 tuple:消息元组,在Spoults和Bolts传递数 ...