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. PHP接收post请求,不是空数组就是没值,怎么办!

    使用: $_POST $_REQUEST I('post.') 都不行, 换成: file_get_contents("php://input");

  2. PHP 扩展 trie-tree, swoole过滤敏感词方案

    在一些app,web中评论以及一些文章会看到一些*等,除了特定的不显示外,我们会把用户输入的一些敏感字符做处理,具体显示为*还是其他字符按照业务区实现. 下面简单介绍下业务处理. 原文地址:小时刻个人 ...

  3. spark+phoenix

    phoenix作为查询引擎,为了提高查询效率,为phoenix表创建了二级索引,而数据是sparkstreaming通过hbase api直接向hbase插数据.那么问题来了,对于phoenix的二级 ...

  4. STM32 HAL库学习系列第6篇---定时器TIM 级联配置

    应用情景 使用定时器配置编码器模式,发现STM32只有两个定时器是32位,16位的测量值不够用,发现是可以使用两个16位定时器级联为32位的. 我是在使用编码器计数电机转速时使用,但是最终实现的效果不 ...

  5. STM32 Startup**.s文件中使用的 __main C函数入口

    代码: ; Reset handler Reset_Handler PROC EXPORT Reset_Handler [WEAK] IMPORT SystemInit IMPORT __main L ...

  6. ubuntu apt源配置

    前言:看见Ubuntu新出了18.04版本感觉不错,装一个玩玩,虽然有很多教程可以参考,但我也给出一个不是很一样的方案吧,尽量解释的详细一点. 为了下载更方便,速度更快,我们往往在使用Linux系列系 ...

  7. ILOVEYOU代码

    代码确实很简单...我是初学者,练手的. /* 文件名: Love.c 描 述: 打印字母和图形 */ #include<stdio.h> #include<windows.h> ...

  8. A1033

    找出最小开销. 思路: 出发点的加油站编号设为0,终点的加油站编号设为n,其他加油站编号按距离依次排序. 如果0号加油站的距离!=0,则无法出发,行驶距离为0. 从起点开始,寻找规则为,如果存在油价小 ...

  9. Oracle下如何设置 log_archive_dest

    一:存在 DB_RECOVERY_FILE_DEST 时,如何设置 LOG_ARCHIVE_DEST: SQL> archive log listデータベース・ログ・モード アーカイブ・モード自 ...

  10. 04-JVM内存模型:直接内存

    1.1.什么是直接内存(Derect Memory) 在内存模型最开始的章节中,我们画出了JVM的内存模型,里面并不包含直接内存,也就是说这块内存区域并不是JVM运行时数据区的一部分,但它却会被频繁的 ...