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 ...
随机推荐
- C - 剪花布条
一块花布条,里面有些图案,另有一块直接可用的小饰条,里面也有一些图案.对于给定的花布条和小饰条,计算一下能从花布条中尽可能剪出几块小饰条来呢? Input输入中含有一些数据,分别是成对出现的花布条和 ...
- Python开发工具安装
v阅读目录 v写在前面 v基本概念 vWindows搭建python开发环境 v从Hello World开始 v博客总结 v博客前言 从大学开始玩python到现在参加工作,已经有5年了,现在的公司是 ...
- 配置Chrome的代理服务器
用命令行启动Chrome,带以下参数: --proxy-server="socks5://myproxy:8080" 下列参数可选 --host-resolver-rules= ...
- 寒城攻略:Listo 教你用 Swift 写IOS UI 项目计算器
之前总结过 Swift 的语言攻略,这里就不做赘述了,如今做一个实例计算器项目来介绍一下 Swift 的应用.(凝视已经全然.直接上代码) 先看一下效果图: 以下是详细的代码和解释: 分享快乐.开源中 ...
- 【安卓笔记】ormlite入门
ps:写这篇文章的目的是尝试下新的markdown编辑器哈哈 简单介绍 ORMLite provides a lightweight Object Relational Mapping between ...
- HDU5489 LIS变形
Removed Interval Problem Description Given a sequence of numbers A=a1,a2,…,aN , a subsequence b1,b2, ...
- Advapi32.dll 函数接口说明
Advapi32.dll 函数接口说明 函数原型 说明 AbortSystemShutDown ...
- uboot 解压缩
在uboot中进行解压缩是非常实用的 uboot中完毕delay 用户进行交互的段 if(BootType == '3') { char *argv[3]; printf(" \n3: ...
- C++<iomanip>控制符
C++<iomanip>控制符 c++ cout 输出格式 在c++程序里面经常见到下面的头文件 #include <iomanip> io代表输入输出,manip是manip ...
- map集合遍历的五种方法
package com.jackey.topic; import java.util.ArrayList;import java.util.HashMap;import java.util.Itera ...