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<iostream> //建立二叉树和dfs
using namespace std;
int maxdeep=0, a=0, b=0;
struct node{
int value;
node* left=NULL;
node* right=NULL;
node(int v):value(v), left(NULL), right(NULL){
}
};
node* insert(int v, node* root, int deep){
if(!root){
node* temp=new node(v);
root =temp;
maxdeep=(deep>maxdeep?deep:maxdeep);
}else{
if(v<=root->value)
root->left=insert(v, root->left, deep+1);
else
root->right=insert(v, root->right, deep+1);
}
return root;
}
void preorder(node* root, int deep){
if(root==NULL)
return;
if(deep==maxdeep-1)
a++;
else if(deep==maxdeep)
b++;
preorder(root->left, deep+1);
preorder(root->right, deep+1); }
int main(){
int n, v;
cin>>n;
node* root=NULL;
for(int i=0; i<n; i++){
cin>>v;
root=insert(v, root, 1);
}
preorder(root, 1);
cout<<b<<" + "<<a<<" = "<<a+b<<endl;
return 0;
}

PAT 1115 Counting Nodes in a BST的更多相关文章

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

  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

    https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...

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

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

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

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

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

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

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

随机推荐

  1. Batch 拷贝远程机器文件到本机指定目录下

    net use * /del /yesNET USE Y: \\远程机IP\d$  登录密码 /user:domain\登录用户 set sourcePath="Y:\DOAutomatio ...

  2. oc75--不可变字典NSDictionary

    // // main.m // NSDictionary // // #import <Foundation/Foundation.h> int main(int argc, const ...

  3. Kubernetes——基于容器技术的分布式架构领先方案,它的目标是管理跨多个主机的容器,提供基本的部署,维护以及运用伸缩

    1.Kubernetes介绍 1.1 简介 Kubernetes是什么?首先,它是一个全新的基于容器技术的分布式架构领先方案.其次,它是一个开放的开发平台.最后,它是一个完备的分布式系统支撑平台.Ku ...

  4. SpringMVC+MyBaties关于上传(跟新)图片的问题

    /** * 方法名:uploadPhoto * 描 述:TODO(上传图片) * 作 者:池彦龙 * 时 间:2017/03/30 * 返回类型: * 参 数: * 异 常: */ Controlle ...

  5. Maven远程中央仓库地址

    阿里云 - http://maven.aliyun.com/nexus/content/groups/public/ Apache Snapshots - https://repository.apa ...

  6. selenium3 + python - iframe

    一.frame和iframe区别 Frame与Iframe两者可以实现的功能基本相同,不过Iframe比Frame具有更多的灵活性. frame是整个页面的框架,iframe是内嵌的网页元素,也可以说 ...

  7. find_in_set的用法(某个字段包含某个字符)

    有个文章表里面有个type字段,他存储的是文章类型,有 1头条,2推荐,3热点,4图文 .....11,12,13等等 现在有篇文章他既是 头条,又是热点,还是图文, type中以 1,3,4的格式存 ...

  8. php微信开放平台--第三方网页微信扫码登录(OAuth2.0)

    第一.OAuth2.0 OAuth(开放授权)是一个开放标准,允许用户让第三方应用访问该用户在某一网站上存储的私密的资源(如照片,视频,联系人列表),而无需将用户名和密码提供给第三方应用. 允许用户提 ...

  9. NS2学习笔记(三)

    代理(Agents) 代理是网络层数据包产生和使用的端点,用于实现多层的协议.Agent类部分用OTcl实现,部分用C++实现.C++的代码在~ns/agent.cc和~ns/agent.h中,OTc ...

  10. 【CodeForces727E/CF727E】Games on a CD (字符串哈希)

    题目: CodeForces727E 分析: 看到字符串比较,肯定想到哈希啊--现学的哈希,先丢两个重要的公式 (\(seed\)是大于字符集大小的质数,\(p\)是大质数) \[hash[i]=(h ...