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 [−1000,1000] 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<vector>
#include<algorithm>
using namespace std;
int n;
int num[1001]{ 0 };
int minx = -1;
struct info {int data;info* left;info* right;
};
void insert(info* &root, int value)
{if (!root){root = new info;root->data = value;root->left = nullptr;root->right = nullptr;return;}if (value <= root->data)insert(root->left, value);if (value > root->data)insert(root->right, value);
}
void dfs(info* root,int depth)
{if (!root)return;if (minx < depth){minx = depth;}num[depth]++;dfs(root->left, depth + 1);dfs(root->right, depth + 1);
}
int main()
{cin >> n;vector<int>v(n);info* root = nullptr;;for (int i = 0; i < n; i++){cin >> v[i];insert(root, v[i]);}dfs(root, 1);cout << num[minx] << " + " << num[minx - 1] << " = " << num[minx] + num[minx - 1];
}

1115 Counting Nodes in a BST(甲级)相关推荐

  1. PAT甲级1115 Counting Nodes in a BST (30分):[C++题解] 递归建二叉搜索树、dfs求一层结点数量

    文章目录 题目分析 题目链接 题目分析 分析 首先本题给定的二叉搜索树的定义和其他地方的不同.本题小于等于的是左子树,右子树是大于根结点的. 然后说一下做题的思路. 给定一串数据,让构造二叉搜索树. ...

  2. 1115. Counting Nodes in a BST (30) 数据结构

    给出一颗BST树 求这个数最后两层的节点数量 用c1+c2 = n 的形式给出 建树深度搜索即可 #include<iostream> #include<cstdio> #in ...

  3. 1115 Counting Nodes in a BST

    我的DFS void DFS(Node* root){if(root==NULL)return;if(root->lchild){root->lchild->layer = root ...

  4. 1115 Counting Nodes in a BST (30 分)【难度: 一般 / 知识点: 构建二叉搜索树】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 很经典的构建搜索二叉树. #include& ...

  5. Two nodes of a BST are swapped, correct the BST

    Two nodes of a BST are swapped, correct the BST[转载] Two of the nodes of a Binary Search Tree (BST) a ...

  6. Two nodes of a BST are swapped, correct the BST(恢复两个节点被交换的BST)

    Two nodes of a BST are swapped, correct the BST(恢复两个节点被交换的BST) Q: BST的两个节点被交换了,修复它: Input Tree:10/ \ ...

  7. 刷PAT甲级的各题思路、细节以及遇到的问题记录

    1001 A+B Format (20分) 因为一定会用到字符串,而string非常好用,但是用的时候一定要注意不能越界访问,否则会在运行时出现abort() has been called. 100 ...

  8. oracle 连接组件,[2021] node连接oracle数据库示例[使用oracle官方组件]

    [2021] node连接oracle数据库示例[使用oracle官方组件] node 连接 oracle 示例 本示例采用的 oracledb 和 instantclient-basic-windo ...

  9. PAT甲级真题目录(按题型整理)(转自柳神)

    转载自:https://www.liuchuo.net/archives/2502?tdsourcetag=s_pcqq_aiomsg 最短路径 1003. Emergency (25)-PAT甲级真 ...

最新文章

  1. [Flutter] 发布自己的插件 package
  2. Oracle创建表空间、创建用户以及授权、查看权限
  3. Android移动开发之【Android实战项目】DAY10-App端耗流量场景问题及减少消耗办法
  4. 使用Tesseract-OCR训练文字识别记录
  5. Linux面试相关 c程序的运行流程
  6. 不止代码:乘法游戏 题解(区间dp)
  7. 变相裁员?人人车发奇葩通知:未离职员工每天打卡三次 只抄规则
  8. php strpose_php中strpos()和stripos()函数的区别用法详解
  9. 几个交换问题的咨询?
  10. php同学录网站设计_基于PHP的校友录系统的设计与实现
  11. input之question
  12. [RK3288][Android6.0] 如何配置MIPI DSI Clock和PCLK
  13. 学习记录:由技术而产品,由产品而商务
  14. Spring Cloud【Finchley】实战-05配置中心的搭建(配合使用Eureka)和Config Server高可用
  15. python求三重积分_三重积分的Python数值计算
  16. 创业公司怎样才能有效的进行员工股权激励
  17. 方舟手游显示服务器超实,全渠道预约超过2200万,《方舟:生存进化手游》开启未来手游新篇章...
  18. android 高德地图 定位蓝点消失,高德地图蓝点定位不显示蓝点
  19. 【Cartographer参数详解,调参,降低计算量调优笔记记录】
  20. QPainter绘制自定义大小的图片

热门文章

  1. 领域应用 | 中医临床术语系统
  2. SSM:web目录下有的jsp和图片不能显示,有的可以显示的解决方法
  3. RabbitMQ从初学到精通一
  4. web移动端开发经验总结
  5. 记忆化搜索 codevs 2241 排序二叉树
  6. Equivalent Strings
  7. Arcgis for javascript不同的状态下自定义鼠标样式
  8. 《Cracking the Coding Interview》——第11章:排序和搜索——题目7
  9. 使用wxWidgets编程——第一步
  10. 牛客15187 分元宵 (快速幂)