https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904

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 <bits/stdc++.h>
using namespace std; int N;
vector<int> v(1000);
int depth = -1; struct Node {
int val;
struct Node *left, *right;
}; Node* BuildBST(Node *root, int x) {
if(!root) {
root = new Node();
root -> val = x;
root -> left = NULL;
root -> right = NULL;
} else if(x <= root -> val)
root -> left = BuildBST(root -> left, x);
else root -> right = BuildBST(root -> right, x); return root;
} void dfs(Node* root, int step) {
if(!root) {
depth = max(depth, step);
return ;
}
v[step] ++;
dfs(root -> left, step + 1);
dfs(root -> right, step + 1);
} int main() {
scanf("%d", &N);
Node *root = NULL;
for(int i = 0; i < N; i ++) {
int num;
scanf("%d", &num);
root = BuildBST(root, num);
}
dfs(root, 0);
printf("%d + %d = %d\n", v[depth - 1], v[depth - 2], v[depth - 1] + v[depth - 2]);
return 0;
}

  先建树然后 dfs 记录每一层的节点数目存在数组里 和上个提交的题目比较类似了

PAT 甲级 1115 Counting Nodes in a BST的更多相关文章

  1. 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 ...

  2. PAT甲级——A1115 Counting Nodes in a BST【30】

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  3. 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 ...

  4. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

  5. 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 tre ...

  6. 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 ...

  7. [二叉查找树] 1115. Counting Nodes in a BST (30)

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  8. 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)

    题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...

  9. PAT 1115 Counting Nodes in a BST

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. Docker技术入门与实战 第二版-学习笔记-6-仓库

    仓库(Repository)是集中存放镜像的地方 一个容易混淆的概念是注册服务器(Registry). 实际上注册服务器是管理仓库的具体服务器,每个服务器上可以有多个仓库,而每个仓库下面有多个镜像. ...

  2. day1-课堂代码

    # # a = 1 # b = a # print(b) # # c = a + 1 # print(c) # # def add(x,y): # return x+y # # d = add(3,5 ...

  3. resnet模型详细结构

    resnet有5个stage,每个stage缩小一倍(即stride2).第1个stage是7*7个卷积大的缩小1倍,第2个stage是通过max-pooling缩小1倍,后面3个stage都是在各自 ...

  4. 回调函数ros::spin()与ros::spinOnce()

    ros::spin() 这句话的意思是循环且监听反馈函数(callback).循环就是指程序运行到这里,就会一直在这里循环了.监听反馈函数的意思是,如果这个节点有callback函数,那写一句ros: ...

  5. IntelliJ IDEA 2018.3发布

    本文转自:https://www.linuxprobe.com/intellij-idea-2018-3-java-12.html

  6. Python2.7-sched

    sched 模块,实现了简单的事件按计划时间表执行的功能,缺点在于运行的时候会占用主线程,直到事件执行完毕,更好的方法是用 threading.Timer 类 创建实例方法: sched.schedu ...

  7. zabbix items 配置

    item是什么?它是我们对于host监控的基本条目,它属于不同的applications中,item的设置既可以针对具体的某个host主机,也可以针对模板进行设定(可以在多个主机进行复用). item ...

  8. STM32 低功耗 调试心得

    MCU在进入STOP模式的时候,GPIO的状态都是保持在进入低功耗模式之前的状态,在最小系统中,MCU的GPIO都是悬空的,所以设置为何种状态都不会影响到功耗.但当连接到外设后,外设的电平状态和所连接 ...

  9. office2016word 每次打开都有进度条问题 解决方式

      最佳答案   每次打开Office 2016都提示配置进度(包括Word.PPT.Excel等等Office产品都有这种现象),如图,先是显示“安装程序正在准备必要的文件”,接着显示“正在配置Mi ...

  10. Storm 安装部署

    环境要求JDK 1.6+java -versionPython 2.6.6+python -V ZooKeeper3.4.5+storm 0.9.4+ 单机模式上传解压 $ .tar.gz $ cd ...