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. go fmt格式化----“占位符”

    https://studygolang.com/articles/2644 https://studygolang.com/static/pkgdoc/pkg/fmt.htm

  2. feign的callback设定后,项目启动错误

    错误如下: Error starting ApplicationContext. To display the auto-configuration report re-run your applic ...

  3. python __all__用法

    主要是用来限定暴露的api a.py文件里面的内容 __all__ = ['major_fun'] def major_fun(): pass def assist_fun(): pass b.py ...

  4. gcc和g++头文件和库路径的寻找和添加

    对所有用户有效修改/etc/profile文件 对个人有效则修改~/.bashrc文件 #在PATH中找到可执行文件程序的路径. export PATH =$PATH:$HOME/bin (可一次指定 ...

  5. hdu5289 2015多校联合第一场1002 Assignment

    题意:给出一个数列.问当中存在多少连续子区间,当中子区间的(最大值-最小值)<k 思路:设dp[i]为从区间1到i满足题意条件的解.终于解即为dp[n]. 此外 如果对于arr[i] 往左遍历 ...

  6. ssh事务回滚,纪念这几个月困扰已久的心酸

    以前的事务采用的是JTA,xml注入的方式.本人就着开发要优雅合理利用轮子的态度,一直不满意JTA式的申明和切入方式. spring的注解方式多优雅,可是万恶的直到项目快要上线时终于找到了注解式不能回 ...

  7. ios开发之--所有设备的屏幕尺寸

    所有设备型号官网地址:https://www.theiphonewiki.com/wiki/Models iPhone: 机型 像素 比例 像素密度 屏幕尺寸 机型代码 发布日期 iPhone 2g ...

  8. 标签a点击以后,5秒内禁止点击,5秒后激活

    方法1:利用bootstrap里面的类disabled,禁止链接 <a href='javascript:onHref()' id="test">点击</a> ...

  9. 【RF库XML测试】测试的XML文件说明

    文件存放路径:C:\workspace\robotframework\test_rf_api\testdata\XML.xml 文件内容: <example> <first id=& ...

  10. [转]linux下释放文件内存

    在Linux系统下,我们一般不需要去释放内存,因为系统已经将内存管理的很好.但是凡事也有例外,有的时候内存会被缓存占用掉,导致系统使用SWAP空间影响性能,此时就需要执行释放内存(清理缓存)的操作了. ...