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 ...
随机推荐
- php无限分类 下拉框
无限分类 下拉框优势:填写参数少,只需要指定一个循环节点($parnent_id),就可以循环所有下级分类.循环输出结构很有特色,比较符合我的口味.补充: $parent_id才是上下级关联的节点,i ...
- BeiDou开源项目
本文主要围绕着BeiDou是什么及其安装和快速开始等两个方面,希望能够对初学者和对此感兴趣的朋友有所帮助. 一. BeiDou是什么 它是服务器呈现的React应用程序的同构框架 特征如下: ✔︎高性 ...
- JSON无限折叠菜单编写
最近看了一篇关于JSON无限折叠菜单的文章 感觉写的不错,也研究了下代码,所以用自己编码方式也做了个demo 其实这样的菜单项在我们网站上或者项目导航菜单项很常见的一种效果,特别是在一些电子商务网上上 ...
- JAVA调用FTP上传文件
import java.io.File; import java.io.FileInputStream; import org.apache.commons.net.ftp.FTP; import o ...
- Spark内部流程图
转载自:https://blog.csdn.net/refuil/article/details/52055104
- 欢迎到我的新Blog!
https://winniechen.cn 里面的页面还不是很好看...争取改一下! 里面的题解大部分也会在这里更新! 谢谢各位捧场!
- JS 01 变量_数据类型_分支循环_数组
点击直通车↓↓↓ 数据类型及数据类型的手动转换 数组 一.概念 JavaScript(JS)是一种基于对象和事件驱动.且可以与HTML标记语言混合使用的脚本语言,其编写的程序可以直接在浏览器中解释执 ...
- 大数据入门第二十二天——spark(三)自定义分区、排序与查找
一.自定义分区 1.概述 默认的是Hash的分区策略,这点和Hadoop是类似的,具体的分区介绍,参见:https://blog.csdn.net/high2011/article/details/6 ...
- ptrace注入型病毒“聊天剽窃手”分析
概述 “聊天剽窃手”Windseeker是一款间谍软件,它使用了ptrace进程注入技术,能够对微信和QQ的聊天记录进行监控. 软件安装后的桌面图标和启动界面如图所示: 行为分析 该应用首先获取手 ...
- 【php增删改查实例】第七节 - 部门管理模块(画一个datagrid表格)
在easyui中,datagrid组件需要用一个table标签去渲染. <table id="grid0" title="部门管理" class=&quo ...