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生成excel表格文件并下载

    本文引自网络,仅供自己学习之用. 利用php导出excel我们大多会直接生成.xls文件,这种方便快捷. function createtable($list,$filename){ header(& ...

  2. MyBatis实战之初步

    关于MyBatis与Hibernate及其JDBC的比较,大家可以参考我的这篇文章:MyBatis+Hibernate+JDBC对比分析 如果觉得这个还不够系统全面,可以自行Google或者百度. 用 ...

  3. css心跳动画

    1.图片无限放大缩小,类似心跳 css如下 @keyframes scaleDraw { /*定义关键帧.scaleDrew关键帧名称*/ 0%{ transform: scale(1); /*开始为 ...

  4. navicat 连接Oracle 报错:Cannot load OCI DLL, 126

    1.64位win7 安装了oracle11g 使用Navicat for Oracle cannot load OCI DLL,126 解决方法:navicat 菜单中 -工具->选项-> ...

  5. CSS之Header--我的头部我做主

    <div class='header'> <div class="header-left"> <span class='iconfont back-i ...

  6. kettle学习笔记(五)——kettle输出步骤

    一.概述 数据库表: • 表输出 • 更新,删除,插入/更新 • 批量加载(mysql,oracle) • 数据同步 文件: • SQL 文件输出 • 文本文件输出 • XML 输出 • Excel ...

  7. Python3入门(八)——面向对象OOP

    一.概述 老生常谈了,万物皆对象.Python作为一门面向对象的语言,也不例外 直接看一个简单的类定义和实例化类的示例: class Student: pass stu = Student() // ...

  8. 20155321 《网络攻防》 Exp2 后门原理与实践

    20155321 <网络攻防> Exp2 后门原理与实践 实验内容 例举你能想到的一个后门进入到你系统中的可能方式? 我觉得人们在平时上网的时候可能会无意识地点击到一些恶意的网站,这些网站 ...

  9. 奔跑吧vivado

        上一节一上来就是Linux,不是炫耀我们的MiZ702能跑Linux,而是为了方便的把外设一次性测试完.大家都知道MiZ702精华在于FPGA与ARM的完美融合,就像太极一样阴阳调和--软中有 ...

  10. 汇编 OR运算

    知识点:  OR运算  逻辑或  按位或 一.OR运算 12||1=1; 1||01=1; 0||0=0; || //逻辑或 | //按位或 int _tmain(int argc, _TCHA ...