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. Sublime text2插件

    Sublime插件: Sublime有好几种安装插件的方法,但是最好用也是最长用的是ctrl+shift+p. 第一步: 使用ctrl+` 调出Sublime控制台,在控制台中输入 import ur ...

  2. python 捕捉错误,exception,traceback和sys.exc_info()比较

    import traceback,sys import requests try : requests.get('dsdsd') ##故意让他出错 except Exception,e: print ...

  3. 【linux】 linux 禁止ping

    linux 禁止 ping 一.修改内核参数 1.临时允许PING操作的命令为:echo 0 >/proc/sys/net/ipv4/icmp_echo_ignore_all 2.永久允许PIN ...

  4. 【动态规划】数字分组I

    [动态规划]数字分组I 时间限制: 1 Sec  内存限制: 64 MB提交: 10  解决: 6[提交][状态][讨论版] 题目描述 给出一堆魔法石的重量,问如何分成两堆,使得它们质量和之差最小,求 ...

  5. Jquery Ajax 和json用法

    向您的页面添加 jQuery 库 jQuery 库位于一个 JavaScript 文件中,其中包含了所有的 jQuery 函数. 可以通过下面的标记把 jQuery 添加到网页中: <head& ...

  6. java.security.NoSuchAlgorithmException: SHA1PRNG SecureRandom not available

    好久没有使用MyEclipse10了,今天打开看了以前大学的项目,在Tomcat7中发布启动,我嚓嘞,报错: SEVERE: Exception initializing random number ...

  7. Splash 对象属性

    args js_enabled resource_timeout images_enabled plugins_enabled scroll_position

  8. Jar命令

    JAR包是Java中所特有一种压缩文档,其实大家就可以把它理解为.zip包;当然也是有区别的,JAR包中有一个META-INF\MANIFEST.MF文件,当你打成JAR包时,它会自动生成. 一.ja ...

  9. windowsError错误码详解

    WindowsError的错误代码详解 0操作成功完成. 1功能错误. 2系统找不到指定的文件. 3系统找不到指定的路径. 4系统无法打开文件. 5拒绝访问. 6句柄无效. 7存储控制块被损坏. 8存 ...

  10. gozmq的安装与使用

    1. 安装zmq 下载Windows版安装或linux版本并执行安装命令: tar zxvf zeromq-4.1.6.tar.gz cd zeromq-4.1.6 ./configure make ...