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
n1
is the number of nodes in the lowest level,n2
is that of the level above, andn
is 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 ...
随机推荐
- N天学习一个linux命令之ssh-keygen
用途 生成ssh加密算法需要使用到的秘钥以及管理和转换 用法 ssh-keygen [-q] [-b bits] -t type [-N new_passphrase] [-C comment] [- ...
- Mac OSX:最简单的安装ant方法
安装ant最简单的方法就是通过brew.步骤如下:1. 安装brew(如果已经安装可以跳过这步).ruby -e "$(curl -fsSL https://raw.github.com/m ...
- Win8.1下COCOS2D-X 3.4环境搭建
Cocos2dx_3.4开发环境搭建,并编译成APK 第一步:须要下载的:(windows64位系统下环境搭建) Ant apache-ant-1.9.4-bin.zip NDK and ...
- 【POJ 2983】Is the Information Reliable?(差分约束系统)
id=2983">[POJ 2983]Is the Information Reliable? (差分约束系统) Is the Information Reliable? Time L ...
- SerializeUtil 序列化,反序列化工具类
package cloud.app.prod.home.utils; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutp ...
- SpringMVC中的 --- 异常处理
系统异常处理器SimpleMappingExceptionResolver 处理器方法执行过程中,可能会发生异常,不想看到错误黄页,想看到一个友好的错误提示页. 自定义异常处理器 使用异常处理注解
- luogu1273 有限电视网
题目大意 有一棵有根树,每个结点有一个收益,每条边有一个花费.如果要选择一个叶子结点,则根节点到该叶子结点的路径上的所有结点都必须被选择.求当总收益大于等于总花费的情况下,最多能选择多少个叶子结点. ...
- [RK3288][Android6.0] U-boot 启动流程小结【转】
本文转载自:http://blog.csdn.net/kris_fei/article/details/52536093 Platform: RK3288OS: Android 6.0Version: ...
- poj 1061(扩展欧几里得定理求不定方程)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...
- [NOIP 2014] 生活大爆炸版石头剪刀布
[题目链接] http://uoj.ac/problem/15 [算法] 按题意模拟即可[代码] #include<bits/stdc++.h> using namespace std; ...