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. Java JVM虚拟机选项Xms/Xmx/PermSize/MaxPermSize(转)

    通过JVM的这些选项:Xms/Xmx/PermSize/MaxPermSize可以牵扯出很多问题,比如性能调优等. 说明:以下转载没经过实践. 经验实例(参考): 设置每个线程的堆栈大小.JDK5.0 ...

  2. 【从0開始Tornado建站】发表文章和评论

            先上个效果图: 这是每一个用户的主页.由于是基本功能.所以用户头像.爱好等信息都还没有,在下一阶段加上.右側"发表新文章"按钮点击后进入发表文章的页面: 之前尝试过 ...

  3. BZOJ 1605 [Usaco2008 Open]Crisis on the Farm 牧场危机 DP

    题意:链接 方法: DP 解析: 第一眼搜索题,复杂度不同意dfs,并且牛的数量太多不能bfs,迭代更不可能,A*不会估价.可能记忆化? 等等记忆化我还搜个毛线- 直接改成DP就好了. 状态非常好想非 ...

  4. A*(也叫A star, A星)寻路算法Java版

    寻路算法有非常多种,A*寻路算法被公觉得最好的寻路算法. 首先要理解什么是A*寻路算法,能够參考这三篇文章: http://www.gamedev.net/page/resources/_/techn ...

  5. 使用golang来设计我们的Ubuntu Scope

    我们知道golang越来越被非常多的开发人员来开发应用.go语言也能够用于开发Ubuntu Scope. 在今天的教程中.我们将具体介绍怎样使用go语言来开发我们的Scope.这对于非常多的不太熟悉C ...

  6. 线段树 hdu3255 Farming

    做了这么多扫描线的题,,基本都是一个思路. 改来改去,,无非就是维护的节点的内容以及push_up越写越复杂了而已 首先将价格排序处理一下编号,变成编号越大的powerfol越大 然后后面加入扫描线的 ...

  7. 新手对ASP.NET MVC的疑惑

    习惯了多年的WEB FORM开发方式,突然转向MVC,一下子懵了,晕头转向,好多不习惯,好多不明白,直到现在也没弄明白,只好先记下来,在应用中一一求解. 主要集中在视图(View)这里. 1.@Htm ...

  8. 5200 fqy的难题----2的疯狂幂

    5200 fqy的难题----2的疯狂幂  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond 题解  查看运行结果     题目描述 Description ...

  9. C++重载运算符简单总结

    当运算符作用于类类型的运算对象时,可以通过运算符重载重新定义该运算符的含义.明智的使用运算符重载能令我们的程序更易于编写和阅读. 一.基本概念 什么是运算符重载?重载的运算符是具有特殊名字的函数:它们 ...

  10. Ubuntu下终端Vim编写C语言程序 AAAAA

    我是开虚拟机下的Ubuntu,装双系统又卸了,Ubuntu默认是不包含编辑器vim和编译器gcc.如果你是刚安装好的Ubuntu电脑,下面我们将来实现自己的第一个程序. 1.准备工作 首先进入root ...