1115 Counting Nodes in a BST (30 分)

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

题意:求二叉查找树最低两层结点,并求出他们的和。

分析:先建树,再层序遍历求出最大层数maxlayer,遍历过程中用num数组来记录每层结点个数即可。还有一种方法是用DFS求出最大深度maxdepth,同样也用数组记录每个深度的结点个数。

 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-27-15.33.36
 * Description : A1115
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 #include<set>
 #include<queue>
 using namespace std;
 ;
 int a[maxn],n;
 int num[maxn];

 struct node{
     int data,layer;
     node* lchild;
     node* rchild;
 };

 node* newNode(int v){
     node* Node=new node;
     Node->data=v;
     Node->lchild=Node->rchild=NULL;
     return Node;
 }
 void insert(node* &root,int x){
     if(root==NULL) {
         root=newNode(x);
         return;
     }
     else if(x>root->data) insert(root->rchild,x);
     else insert(root->lchild,x);
 }
 node* Create(int data[],int n){
     node* root=NULL;
     ;i<n;i++){
         insert(root,data[i]);
     }
     return root;
 }
 ;
 void BFS(node* root){
     queue<node*> q;
     q.push(root);
     root->layer=;
     while(!q.empty()){
         node* now=q.front();
         q.pop();
         num[now->layer]++;
         if(maxlayer<now->layer) maxlayer=now->layer;
         if(now->lchild){
             now->lchild->layer=now->layer+;
             q.push(now->lchild);
         }
         if(now->rchild){
             now->rchild->layer=now->layer+;
             q.push(now->rchild);
         }
     }
 }

 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     scanf("%d",&n);
     ;i<n;i++){
         scanf("%d",&a[i]);
     }
     node* root=Create(a,n);
     BFS(root);
     printf(],num[maxlayer]+num[maxlayer-]);

     ;
 }
 /**
 * Copyright(c)
 * All rights reserved.
 * Author : Mered1th
 * Date : 2019-02-06-00.18.21
 * Description : A1115
 */
 #include<cstdio>
 #include<cstring>
 #include<iostream>
 #include<cmath>
 #include<algorithm>
 #include<string>
 #include<unordered_set>
 #include<map>
 #include<vector>
 using namespace std;
 ;
 struct node{
     int data;
     node* lchild;
     node* rchild;
 };

 node* newNode(int v){
     node* Node=new node;
     Node->data=v;
     Node->lchild=Node->rchild=NULL;
     return Node;
 }
 void insert(node* &root,int x){
     if(root==NULL) {
         root=newNode(x);
         return;
     }
     else if(x>root->data) insert(root->rchild,x);
     else insert(root->lchild,x);
 }
 node* Create(int data[],int n){
     node* root=NULL;
     ;i<n;i++){
         insert(root,data[i]);
     }
     return root;
 }
 },maxdepth=-;
 void DFS(node* root,int depth){
     if(root==NULL){
         maxdepth=max(maxdepth,depth);
         return;
     }
     num[depth]++;
     DFS(root->lchild,depth+);
     DFS(root->rchild,depth+);
 }
 int main(){
 #ifdef ONLINE_JUDGE
 #else
     freopen("1.txt", "r", stdin);
 #endif
     int n,data[maxn];
     cin>>n;
     ;i<n;i++){
         cin>>data[i];
     }
     node* root=Create(data,n);
     DFS(root,);
     printf(],num[maxdepth-],num[maxdepth-]+num[maxdepth-]);

     ;
 }

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. [二叉查找树] 1115. Counting Nodes in a BST (30)

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

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

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

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

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

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

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

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

  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. 玩转X-CTR100 l STM32F4 l DS18B20单总线温度传感器

    我造轮子,你造车,创客一起造起来!塔克创新资讯[塔克社区 www.xtark.cn ][塔克博客 www.cnblogs.com/xtark/ ]      本文介绍X-CTR100控制器 扩展DS1 ...

  2. 获取exe所在目录路径,速度

    // test.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <Windows.h> #include &l ...

  3. 新手小白Linux(Centos6.5)部署java web项目(mongodb4.0.2安装及相关操作)

    红帽企业或CentOS的Linux上安装MongoDB的社区版: https://docs.mongodb.com/manual/tutorial/install-mongodb-on-red-hat ...

  4. 转:window.orientation判断移动设备横屏竖屏

    通过window.orientation来判断设备横竖屏 function checkOrient() { if (window.orientation == 0 || window.orientat ...

  5. Ubuntu中敲写c语言步骤

    1.进入Ubuntu界面 1.1按快捷键 Ctrl + Alt + T: 1.2 安装vim:输入 sudo apt-get install vim: 1.3 安装gcc:输入 sudo apt-ge ...

  6. 在VM上安装centOS后的网络配置

    花了点时间,研究了下VM上的linux虚拟机的网络配置问题.1.环境主机:winXP SP2,家庭宽带,局域网连到路由器,ip地址为192.168.1.101. 虚拟机:centOS(redhat l ...

  7. ballerina 学习二十九 数据库操作

    ballerina 数据操作也是比较方便的,官方也我们提供了数据操作的抽象,但是我们还是依赖数据库驱动的. 数据库驱动还是jdbc模式的 项目准备 项目结构 ├── mysql_demo │ ├── ...

  8. .NET基金会成立

    作者 Jonathan Allen ,译者 臧秀涛 发布于 2014年4月8日 随着.NET基金会的成立,微软在成为主要的开源参与者的道路上又前进了一步.该基金会的宗旨是“成为越来越多的开源.NET项 ...

  9. STL版本号简单介绍

    说明:本文仅供学习交流.转载请标明出处.欢迎转载! 本文的參考文献为:<STL源代码剖析>侯捷        (1)HP STL:全部STL的祖先版本号,由C++之父Alexander S ...

  10. Maven的dependency type属性

    官方地址: http://maven.apache.org/ref/3.5.2/maven-model/maven.html (搜索:Some examples are jar, war, ejb-c ...