PAT 甲级 1115 Counting Nodes in a BST
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的更多相关文章
- 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】
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- 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 ...
- 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 ...
- 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 ...
- [二叉查找树] 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 (30分)(二叉查找树)
题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...
- PAT 1115 Counting Nodes in a BST
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- kubernetes lable 操作
我们要给已创建的Pod“redis-master”添加一个标签role=master: $ kubectl label pod redis-master role=master 查看该Pod的Labe ...
- JS编写日历控件(支持单日历 双日历 甚至多日历等)
前言: 最近几天都在研究日历控件编写,当然前提我要说明下,当然看过别人写的源码 所以脑子一热 就想用自己的编码方式 来写一套可扩展性 可维护性 性能高点的代码控件出来,就算练习练习下,所以前几天晚上下 ...
- jsp el的内置对象
一.el内置对象(11个): pageScope (掌握) requestScope (掌握) applicationScope (掌握) sessionScope (掌握) param (了解) p ...
- kubernetes 禁用虚拟内存 swapoff -a ----- 顺便复习sed 命令
1.如果不关闭swap,就会在kubeadm初始化Kubernetes的时候报错,如下图: [ERROR Swap]: running with swap on is not supported. P ...
- shell杂记
(本文将持续更新)从2015年9月25日开始正式学习linux类的东西. 书籍入门:UNIX.Shell编程24学时教程(中文版).Linux与UNIX Shell编程指南.shell十三问.LINU ...
- 基于Python自动发送QQ群消息
1.准备工作 此次测试基于python3,需要安装qqbot.bs4.requests库. qqbot项目地址:https://github.com/pandolia/qqbot.git pip qq ...
- Delphi XE 10.2.3使用CEF4Delphi取网页元素时碰到 变问号?的处理
用CEF4Delphi取网页元素时碰到ElementInnerText里含有" " 比如网页源码里是"内容 "取出来显示就变成"内容?" 搜 ...
- 大数据入门第二十二天——spark(三)自定义分区、排序与查找
一.自定义分区 1.概述 默认的是Hash的分区策略,这点和Hadoop是类似的,具体的分区介绍,参见:https://blog.csdn.net/high2011/article/details/6 ...
- 20155206 Exp8 WEB基础实践
20155206 Exp8 WEB基础实践 基础问题回答 (1)什么是表单 表单在网页中主要负责数据采集功能. 一个表单有三个基本组成部分: 表单标签:这里面包含了处理表单数据所用CGI程序的URL以 ...
- python变量名感悟
我感悟的是python的变量名其实就可以理解为C/C++中的指针! 1.python的变量在使用之前必须赋值,就像指针在使用之前不能为空. 2.python的内存可以用del释放,C++可以用dele ...