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. PHP生成有背景的二维码图,摘自网络

    有一天产品MM高高兴兴的走过来,兴奋的和我分享她想出来的一个新的idea. 产品MM:你看这个(她指了指她的手机),一脸兴奋 那是一张带着二维码的图片,内容如下: 她接着说:如果我们的分销也能做成类似 ...

  2. day3-课堂代码

    # a = ('哈哈', 'xixi', 'hehe') # print(a[0]) # print(a[0:2]) # # # 列表 # a = ['哈哈', 'xixi', 'hehe', 1, ...

  3. 静态工厂方法和实例工厂方法及普通的bean

    容纳你的bean  bean工厂:最简单的容器,提供了基础的依赖注入支持.创建各种类型的Bean.  应用上下文(ApplicationContext):建立在bean工厂基础之上,提供系统架构服务. ...

  4. POJ3468(线段树区间求和+区间查询)

    https://vjudge.net/contest/66989#problem/C You have N integers, A1, A2, ... , AN. You need to deal w ...

  5. Python 函数(二)

    Python 3 函数(匿名函数.偏函数 and 变量作用域:全局变量.局部变量) 一.匿名函数:没有名字,也不再使用 def 语句这样标准的形式定义的一个函数. OCP培训说明连接:https:// ...

  6. VBA读取、增加自定义和修改文档属性

    读取系统文档属性 Sub read()On Error Resume Nextrw = 1Worksheets(1).ActivateFor Each p In ActiveWorkbook.Buil ...

  7. Go语言安全编码规范-翻译(分享转发)

    Go语言安全编码规范-翻译 本文翻译原文由:blood_zer0.Lingfighting完成 如果翻译的有问题:联系我(Lzero2012).匆忙翻译肯定会有很多错误,欢迎大家一起讨论Go语言安全能 ...

  8. 64位RHEL5系统上运行yum出现"This system is not registered with RHN”的解决方法

    在红帽EL5上运行yum,提示“This system is not registered with RHN”,意思是没有在官网上注册,不能下载RH的软件包,替代方案是采用centos源. 1.卸载r ...

  9. 20155207 《网络对抗技术》EXP3 免杀原理与实践

    20155207 <网络对抗技术>EXP3 免杀原理与实践 基础问题回答 杀软是如何检测出恶意代码的? - 根据特征码进行检测(静态) - 启发式(模糊特征点.行为 ) - 根据行为进行检 ...

  10. 20155223 Exp8 WEB基础实践

    20155223 Exp8 WEB基础实践 基础问题回答 什么是表单? 表单是一个包含表单元素的区域. 表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选框等等)输入信息的元素. 表单使 ...