PAT_A1115#Counting Nodes in a BST
Source:
Description:
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
n1is the number of nodes in the lowest level,n2is that of the level above, andnis the sum.
Sample Input:
9
25 30 42 16 20 20 35 -5 28
Sample Output:
2 + 4 = 6
Keys:
Attention:
- BST定义有时候会不一样,等号跟左子树还是右子树要看清楚,注意审题
Code:
/*
Data: 2019-06-26 15:55:13
Problem: PAT_A1115#Counting Nodes in a BST
AC: 17:15 题目大意:
BST定义:lchild <= root < rchild
根据插入序列建立BST树,统计最底层和次底层的结点数量 基本思路:
建树,记录结点层次,全局变量记录树的最大深度
更新最大深度的同时,统计底层和次底层的结点个数
*/
#include<cstdio>
int deep=,n1=,n2=;
struct node
{
int data;
node *lchild,*rchild;
}; void Insert(node *&root, int x, int height)
{
if(root == NULL)
{
if(deep == height)
n1++;
else if(deep == height+)
n2++;
else if(deep == height-)
{
deep++;
n2 = n1;
n1 = ;
}
root = new node;
root->data = x;
root->lchild = root->rchild = NULL;
}
else if(x <= root->data)
Insert(root->lchild, x, height+);
else
Insert(root->rchild, x, height+);
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE int n,x;
scanf("%d", &n);
node *root = NULL;
for(int i=; i<n; i++)
{
scanf("%d", &x);
Insert(root,x,);
}
printf("%d + %d = %d", n1,n2,n1+n2); return ;
}
PAT_A1115#Counting Nodes in a BST的更多相关文章
- PAT1115:Counting Nodes in a BST
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- 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[构建BST]
1115 Counting Nodes in a BST(30 分) A Binary Search Tree (BST) is recursively defined as a binary tre ...
- A1115. Counting Nodes in a BST
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- 1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
随机推荐
- MOS文章翻译
http://blog.csdn.net/column/details/msdnchina.html?&page=1 http://blog.csdn.net/staricqxyz/artic ...
- 目标跟踪学习系列十:Struck:Structured Output Tracking with Kernels 代码调试
本来想看完代码再具体的写的.可是有人问了就先贴出来吧! 代码调试中会遇到的一些的问题. 首先,你没有代码的话能够在这里下载:http://download.csdn.net/detail/u01219 ...
- ZOJ Monthly, November 2012
A.ZOJ 3666 Alice and Bob 组合博弈,SG函数应用 #include<vector> #include<cstdio> #include<cstri ...
- wpf 样式的调用
这个针对异地调用: 1.在主程序的项目中新建一个Skins的目录.然后再目录里新建一个BlackSkin.xaml的字典资源: <ResourceDictionary xmlns="h ...
- HDU 3718 Similarity(KM最大匹配)
HDU 3718 Similarity 题目链接 题意:给定一个标准答案字符串,然后以下每一行给一个串.要求把字符一种相应一种,要求匹配尽量多 思路:显然的KM最大匹配问题,位置相应的字符连边权值+1 ...
- Android热更新实现原理
最近Android社区的氛围很不错嘛,连续放出一系列的android动态加载插件和热更新库,这篇文章就来介绍一下Android中实现热更新的原理. ClassLoader 我们知道Java在运行时加载 ...
- Spark之MLlib
目录 Part VI. Advanced Analytics and Machine Learning Advanced Analytics and Machine Learning Overview ...
- 网上订餐系统的SQL SERVER 2005数据库连接
- go 函数高级运用
一.函数作用域 说明 作用域的定义:已声明标识符所表示的常量.类型.变量.函数或包在源代码中的作用范围 在说函数作用域之前,先简单说一下局部变量和局变量 简单的说: 全局变量:在一个文件中,任何地方都 ...
- NPM 国内镜像使用方法
npm官方站点: http://www.npmjs.org/ 本文使用国内镜像地址: http://www.cnpmjs.org/ 搜索镜像:https://npm.taobao.org/ 具体方法: ...