1115 Counting Nodes in a BST (30 分)

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 (≤1000) which is the size of the input sequence. Then given in the next line are the N integers in [−10001000] 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

题意:

给定n个数,建一棵二叉搜索树。问倒数第一层和倒数第二层分别有多少个节点。

思路:

先建树,然后dfs看每个节点的深度。

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = ;
int n, num[maxn], cnt[maxn], dep = -;
struct node{
int val;
int left = -, right = -;
int height;
}tree[maxn];
int tot; void add(node t, int rt)
{
if(t.val > tree[rt].val && tree[rt].right != -){
add(t, tree[rt].right);
}
else if(t.val > tree[rt].val){
tree[tot] = t;
tree[rt].right = tot++;
}
else if(tree[rt].left != -){
add(t, tree[rt].left);
}
else{
tree[tot] = t;
tree[rt].left = tot++;
}
} void dfs(int rt, int h)
{
tree[rt].height = h;
cnt[h]++;
dep = max(dep, h);
if(tree[rt].left != -){
dfs(tree[rt].left, h + );
}
if(tree[rt].right != -){
dfs(tree[rt].right, h + );
}
} int main()
{
scanf("%d", &n);
scanf("%d", &tree[tot++].val);
for(int i = ; i < n; i++){
node t;
scanf("%d", &t.val);
add(t, );
} //printf("!\n");
dfs(, );
int n1 = cnt[dep], n2 = cnt[dep - ];
printf("%d + %d = %d\n", n1, n2, n1 + n2);
return ;
}

PAT甲1115 Counting Nodes in a BST【dfs】的更多相关文章

  1. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

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

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

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

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

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

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

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

  7. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  8. PAT 1115 Counting Nodes in a BST

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

  9. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

随机推荐

  1. OpenSift源代码编译过程记录

    本文记录了在CentOS6.5上编译Sift的开源实现OpenSift的编译过程,同一时候记录了编译过程中的几个问题. sift的理论已经有非常多了,以下会给出链接: 1.Requirements a ...

  2. java之 ------ 可变參数和卫条件

    可变參数:适用于參数个数不确定.类型确定的情况,java把可变參数当做数组处理. 可变參数必须位于最后一项.当可变參数个数多于一个时,必将有一个不是最后一项,所以仅仅支持有一个可变參数. 可变參数的书 ...

  3. 设置js同源

    1)设置 document.domain 成一样的就行了(前提是都是同一个顶级域名) 2)例如,域名1:news.xxx.com ,域名2:member.xxx.com,这时可以设置它们的 docum ...

  4. laravel 5.3升级5.4

    1)修改 composer 配置文件 composer.json 1.如果你用了 laravel-admin,larvel-admin 版本改 1.4.x-dev 2.laravel 版本改 5.4. ...

  5. HttpClient(三)-- 抓取图片

    使用HttpClient抓取图片,先通过 entity.getContent() 获取输入流,然后 使用 common io 中的文件复制 方法 将图片专区到本地,代码如下: 1.需要依赖common ...

  6. Splash scroll_position 属性

    scroll_position属性用于控制页面上下或左右滚动,如下,表示控制页面向下滚动 400 像素值并返回结果图, function main(splash, args) assert(splas ...

  7. log4net日志的简单配置

    说起来log4net,我一直都知道这个的存在,但实际在项目中还真是没有去自己写过的那,这一次我在项目完成后并没有着急下一个项目的开始,于是突然想起来是否添加一个日志的编写,于是开始了log4net的总 ...

  8. linux C 调用shell程序执行

    #include<stdio.h> #include <unistd.h> #include <sys/types.h> #include <stdlib.h ...

  9. MFC框架程序解析

    MFC的 程序框架: WinMain函数:程序首先到达全局变量theApp,再到达theAPP的构造函数,最后到达WinMain函数处. 问:为何要定义一个全局对象theAPP,让其在WinMain函 ...

  10. phaser相关

    phaser.js这个插件,中文翻译的开发文档还在翻译中,至于英文的开发文档,勉勉强强查阅,有些方法名和开发文档的有着一些区别,开发文档上时带着er的.不过大体上还是一一对应查找的到的 eg:load ...