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计算出最后两 ...
随机推荐
- wp api jwt 403 (Forbidden) -- JWT is not configurated properly, please contact the admin
需要在 wp-config.php 文件中配置 define('JWT_AUTH_SECRET_KEY', 'your-top-secrect-key'); 參考 403 (Forbidden) -- ...
- python异常整理
一.操作excel 时异常 1.PermissionError: [Errno 13] Permission denied (1)原因:权限错误:[Errno 13] 权限被拒绝 错误产生的原因是文件 ...
- jQuery实现全选与全部选
为了便于用户理解,直接粘贴下面的代码即可 <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- Servlet 上传图片
目录 Servlet 上传图片 预备 需求包 pom 前端代码 Servlet 上传图片 预备 需求包 commons-fileupload -用于上传 jstl -用于jsp页面遍历 servlet ...
- Java标识符&关键字
1. 标识符&关键字 [标识符]: Java 对各种变量.方法和类等要素命名时使用的字符序列称为标识符. 凡是自己可以起名字的地方都叫标识符 命名规则:(一定要遵守,不遵守就会报编译的错误) ...
- ST表 (模板) 洛谷3865
题目背景 这是一道ST表经典题——静态区间最大值 请注意最大数据时限只有0.8s,数据强度不低,请务必保证你的每次查询复杂度为 O(1) O(1) 题目描述 给定一个长度为 N N 的数列,和 M M ...
- redis笔记--------Jedis使用
redis安装和启动就不说了 一.准备工作 1.redis -cli -p 6379 2.eclipse中新建项目,并导入jedis相关包 3.测试jedis连通性 二.Jedis常用API (哈希) ...
- C/Python/Java环境变量配置
链接 全图预览: Java: 只需添加下面三个环境变量即可使用Java.对照这我的添加就行. CLASSPATH的内容: .;%JAVA_HOME%\lib\dt.jar;%JAVA_HOME%\li ...
- map、filter、forEach、reduce数组方法的封装
1.map方法的封装 Array.prototype.mapAlley = function(callback){ //获取调用mapAlley这个方法的数组 let arr = thi ...
- jquery的attr获取表单checked 布尔值问题
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...