1115. Counting Nodes in a BST (30)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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 (<=1000) which is the size of the input sequence. Then given in the next line are the N integers in [-1000 1000] 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
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std; const int maxn=; struct Node
{
int data;
int layer;
Node *lchild,*rchild;
}; void insert(Node * & root,int data)
{
if(root==NULL)
{
root=new Node;
root->lchild=NULL;
root->rchild=NULL;
root->data=data;
return ;
}
if(root->data<data) insert(root->rchild,data);
else insert(root->lchild,data);
} int max_layer=;
int layer[maxn]={}; void layerOrder(Node * root)
{
queue<Node *> q;
root->layer=;
q.push(root);
while(!q.empty())
{
Node * now=q.front();
q.pop();
if(now->layer>max_layer) max_layer=now->layer;
layer[now->layer]+=;
if(now->lchild!=NULL)
{
now->lchild->layer=now->layer+;
q.push(now->lchild);
}
if(now->rchild!=NULL)
{
now->rchild->layer=now->layer+;
q.push(now->rchild);
}
}
} int main()
{
int n;
cin>>n;
Node * root=NULL;
for(int i=;i<n;i++)
{
int input;
cin>>input;
insert(root,input);
}
layerOrder(root);
int a=layer[max_layer];
int b=layer[max_layer-];
cout<<a<<" + "<<b<<" = "<<a+b<<endl;
return ;
}

[二叉查找树] 1115. Counting Nodes in a BST (30)的更多相关文章

  1. 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)

    题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...

  2. PAT Advanced 1115 Counting Nodes in a BST (30) [⼆叉树的遍历,BFS,DFS]

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

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

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

  4. PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)

    题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...

  5. PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】

    题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...

  6. PAT (Advanced Level) 1115. Counting Nodes in a BST (30)

    简单题.统计一下即可. #include<cstdio> #include<cstring> #include<cmath> #include<vector& ...

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

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

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

随机推荐

  1. 使用CURL实现GET和POST方式请求

    /** 使用curl方式实现get或post请求@param $url 请求的url地址@param $data 发送的post数据 如果为空则为get方式请求return 请求后获取到的数据 */f ...

  2. IAR升级之后,编译stm32官方工程报错的解决办法

    IAR升级之后,打开stm32官方例程,编译时提示如下错误: Error[Pe147]: declaration is incompatible with "__nounwind __int ...

  3. Python中各种进制之间的转化

    1.十进制转化为其它进制 (1)bin(x):十进制转化为二进制 [实例1] x=bin(20)   # x的值为字符串'0b10100' (2)oct(x):十进制转化为八进制 [实例2] x=oc ...

  4. Bugku一段base64

    本文转自:本文为博主原创文章,如有转载请注明出处,谢谢. https://blog.csdn.net/pdsu161530247/article/details/74640746 链接中高手给出的解题 ...

  5. 中国大学MOOC-JAVA学习(浙大翁恺)—— 温度转换

    import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto- ...

  6. POJ_1679_The Unique MST(次小生成树)

    Description Given a connected undirected graph, tell if its minimum spanning tree is unique. Definit ...

  7. Java技术——Interface与abstract类的区别

    )抽象类是对类抽象,是面向整个类的自下而上的设计理念,一般是先有各种子类,再有把这些有关系的子类加以抽象为父类的需求.而接口是对行为的抽象,是面向行为的自上而下的设计理念,接口根本就不需要知道子类的存 ...

  8. RHCSA-day4

    硬盘分区 1.硬盘的物理组成 硬盘实际上是由很多的盘片.磁臂.磁头与主轴马达所组成的. 那么实际的数据当然是写在具有磁性物质的盘片上了.数据的读写主要是通过在磁臂上的磁头来完成的.实际运转时,主轴马达 ...

  9. 【LG5055】可持久化文艺平衡树

    [LG5055]可持久化文艺平衡树 题面 洛谷 题解 终于不可以用\(Trie\)水了... 和普通的\(FHQ\;treap\)差不多 注意一下\(pushdown\).\(split\)要新开节点 ...

  10. MYSQL中日期与字符串间的相互转换

    一.字符串转日期 下面将讲述如何在MYSQL中把一个字符串转换成日期: 背景:rq字段信息为:20100901 1.无需转换的: SELECT * FROM tairlist_day WHERE rq ...