Source:

PAT A1115 Counting Nodes in a BST (30 分)

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, and n 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的更多相关文章

  1. PAT1115:Counting Nodes in a BST

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  2. 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 ...

  3. 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 ...

  4. [二叉查找树] 1115. Counting Nodes in a BST (30)

    1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...

  5. 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 ...

  6. A1115. Counting Nodes in a BST

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

  7. 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 ...

  8. PAT 甲级 1115 Counting Nodes in a BST

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

  9. 1115. Counting Nodes in a BST (30)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. kendo Grid的toolbar自定义

    由于这个toolbar官方进增加了create,save还有一个是_____ 所以想要自定义话就需要使用 下面的代码(这个是MVVM模式) data-toolbar='[{ template: Ken ...

  2. C语言实现的lisp解析器介绍

    近期.由于Perl而学习函数式编程, 再进一步学习lisp, 真是一学习就发现自己的渺小. 无意中找到了一个很easy的C语言版的, lisp解析器. 代码非常短, 却非常见功底, 涨姿势了. 附带还 ...

  3. 【转】C语言条件编译及编译预处理阶段

    原文: http://www.cnblogs.com/rusty/archive/2011/03/27/1996806.html 1. 宏定义(宏代换,宏替换,宏: 宏定义是C语言提供的3中预处理功能 ...

  4. oracle强化练习之单行函数

    1.      显示dname和loc中间用-分隔 Select dname ||'-'|| loc From dept; 2.      将部门名称左填充为10位 Select lpad( dnam ...

  5. Unique Paths I,II

    题目来自于:https://leetcode.com/problems/unique-paths/ :https://leetcode.com/problems/unique-paths-ii/ A ...

  6. Python内置的字符串处理函数

    生成字符串变量 str='python String function'   字符串长度获取:len(str) 例:print '%s length=%d' % (str,len(str)) 连接字符 ...

  7. gradle打包android (实现外部导入签名文件、多渠道打包、导入ant脚本)

    近期一直在做android自己主动打包,之前已经完毕了用纯命令行的形式打包.原生态ant脚本打包.和基于android的SDK的打包.而且实现了多渠道打包,后来同事推荐了gradle,网上的资料说gr ...

  8. VIM 移动

    基础 字符移动 k 上移 k h 左移 h l l 右移 j j 下移 你也可以使用键盘上的方向键来移动,但这么做h j k l的存在就失去了意义 之所以使用h j k l来控制方向,其主要目的是让你 ...

  9. saltstack结合Elasticsearch来做salt运行结果展现

    salt尽管好用可是机器管理的越来越多,通过cli的结果输出方式查看运行结果越来越多不能满足我的需求.并且作为一个推动运维自己主动化的攻城狮,使用这样的人眼查看运行结果的方式简直土到掉渣.尽管别人看起 ...

  10. Wikioi 1081 线段树成段更新单点查询

    线段树练习飘逸的写法,自从自己改成这样的写法之后,线段树就没再练过,如今最终练得上了. 由于这里查询仅仅是查询了叶子结点,所以pushUp函数就用不上了,只是我没去掉之前是3ms.去掉之后反而变成4m ...