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 [−10001000] 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 <stdio.h>
#include <algorithm>
#include <set>
#include <vector>
#include <queue>
using namespace std;
int height;
struct node{
int data,h,lvl;
node* l,*r;
};
node* newnode(int x){
node* root = new node;
root->data=x;
root->l=NULL;
root->r=NULL;
root->h=;
return root;
}
int geth(node* root){
if(root==NULL) return ;
return root->h;
}
void updateh(node* root){
root->h = max(geth(root->l),geth(root->r))+;
}
void insert(node* &root,int x){
if(root==NULL) {
root=newnode(x);
return;
}
if(x>root->data){
insert(root->r,x);
updateh(root);
}
else if(x<=root->data){
insert(root->l,x);
updateh(root);
}
}
int main(){
int n;
scanf("%d",&n);
node* root = NULL;
for(int i=;i<n;i++){
int x;
scanf("%d",&x);
insert(root,x);
}
height=root->h;
int n1=,n2=;
queue<node*> q;
root->lvl=;
q.push(root);
while(!q.empty()){
node* now=q.front();
q.pop();
//printf("%d %d\n",now->data,now->lvl);
if(now->l!=NULL){
now->l->lvl=now->lvl+;
q.push(now->l);
}
if(now->r!=NULL){
now->r->lvl=now->lvl+;
q.push(now->r);
}
if(now->lvl==height)n1++;
if(now->lvl==height-)n2++;
}
printf("%d + %d = %d",n1,n2,n1+n2);
}

注意点:二叉搜索树的建立与层序遍历。不过好像做麻烦了,用dfs会更简洁。又好像dfs都不用,可以直接在插入时候加个lvl数组算

PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs的更多相关文章

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

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

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

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

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

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

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

  5. [leetcode]333. Largest BST Subtree最大二叉搜索树子树

    Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...

  6. BST | 1064 完全二叉搜索树

    OJ:https://www.patest.cn/contests/pat-a-practise/1064 (一)23分(3个case未过)代码 建树的规律是我瞎猜的.首先用样例数据分析. 对数据排序 ...

  7. PAT甲级——A1115 Counting Nodes in a BST【30】

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

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

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

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

随机推荐

  1. 【Tomcat】详解tomcat的连接数与线程池

    前言 在使用tomcat时,经常会遇到连接数.线程数之类的配置问题,要真正理解这些概念,必须先了解Tomcat的连接器(Connector). Connector的主要功能,是接收连接请求,创建Req ...

  2. 通过AccessKey调用阿里云CDN接口刷新CDN资源案例

    通过AccessKey远程调用阿里云CDN接口,快速实现自动化集成部署. CdnService.java package com.nfky.cdn; import com.aliyuncs.Defau ...

  3. 初学CSS-2-文本的属性

    文本装饰属性: 格式:text-decoration:underline: 取值:underline(下划线) line-through(删除线) overline(上划线) none(什么都没有) ...

  4. CSS-高度塌陷问题

    目录 CSS-高度塌陷问题 表现 产生的原因 高度塌陷的解决办法: BFC相关 CSS-高度塌陷问题 表现 例如: HTML: <div class="first"> ...

  5. canvas-star4.html

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 什么是DDoS攻击?DDoS防御的11种方针详解

    对于遭受DDOS攻击的情况是让人很尴尬的,如果我们有良好的DDoS防御方法,那么很多问题就将迎刃而解,我们来看看我们有哪些常用的有效地方法来做好DDoS防御呢. 对于DDoS防御的理解: 对付DDOS ...

  7. recovery uncrypt功能解析(bootable/recovery/uncrypt/uncrypt.cpp)

    我们通常对一个文件可以直接读写操作,或者普通的分区(没有文件系统)也是一样,直接对/dev/block/boot直接读写,就可以获取里面的数据内容了. 当我们在ota升级的时候,把升级包下载到cach ...

  8. SQL Server 2012 读写分离设置 - AlsoIn

    原文转至:http://www.tuicool.com/articles/a6rmiam/ 引用: http://technet.microsoft.com/zh-cn/library/jj16176 ...

  9. [20171206]rman与truncate.txt

    [20171206]rman与truncate.txt --//昨天下班在回家的路上,突然想起以前遇到的问题,就是truncate表后,rman做备份时会备份多少truncate表的信息,--//当时 ...

  10. gitlab hooks配置

    1.邮件格式过滤 pre-recieive rev_type=commit # Only check the first commit information due to a lot of comm ...