PAT A1115 Counting Nodes in a BST (30 分)——二叉搜索树,层序遍历或者dfs
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的更多相关文章
- PAT甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- 【PAT甲级】1115 Counting Nodes in a BST (30分)(二叉查找树)
题意: 输入一个正整数N(<=1000),接着输入N个整数([-1000,1000]),依次插入一棵初始为空的二叉排序树.输出最底层和最底层上一层的结点个数之和,例如x+y=x+y. AAAAA ...
- [二叉查找树] 1115. Counting Nodes in a BST (30)
1115. Counting Nodes in a BST (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Y ...
- 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 ...
- [leetcode]333. Largest BST Subtree最大二叉搜索树子树
Given a binary tree, find the largest subtree which is a Binary Search Tree (BST), where largest mea ...
- BST | 1064 完全二叉搜索树
OJ:https://www.patest.cn/contests/pat-a-practise/1064 (一)23分(3个case未过)代码 建树的规律是我瞎猜的.首先用样例数据分析. 对数据排序 ...
- 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 ...
- 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 ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
随机推荐
- 【Java并发编程】18、PriorityBlockingQueue源码分析
PriorityBlockingQueue是一个基于数组实现的线程安全的无界队列,原理和内部结构跟PriorityQueue基本一样,只是多了个线程安全.javadoc里面提到一句,1:理论上是无界的 ...
- What is The Rule of Three?
Question: What does copying an object mean? What are the copy constructor and the copy assignment op ...
- SpringCloud初体验之Eureka
Eureka简介 SpringBoot简化了Spring工程的复杂度,之前复杂的Spring工程被拆分成了一个个小的SpringBoot工程.那么SpringBoot之间如何通讯,相互获取信息呢?这就 ...
- LSApplicationQueriesSchemes--关于info.plist 第三方登录 添加URL Schemes白名单
近期苹果公司iOS 9系统策略更新,限制了http协议的访问,此外应用需要在“Info.plist”中将要使用的URL Schemes列为白名单,才可正常检查其他应用是否安装. 当你的应用在iOS 9 ...
- CSS效果:CSS实用技巧制作三角形以及箭头效果
实现如图所示的三角形图标: html代码如下: <div class="arrow-up"></div> <div class="arrow ...
- 【读书笔记】iOS-分类与协议
分类与协议是Object-C特有概念,分类(Category)可以认为是一种继承性的扩展,而协议(Protocol)可以理解为Java中的Interface(接口)或者C++的纯虚类. 参考资料:&l ...
- CMake笔记(一)
CMake,比makefile更方便,虽然手写makefile同样重要 CMake的所有命令均出现在CMakeList.txt中,注意名字不要写错,并置于源代码目录中 # CMake 最低版本号要求 ...
- SpringBoot取出信息
1.打印头信息 @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody publ ...
- Android View体系(四)从源码解析Scroller
在Android View体系(二)实现View滑动的六种方法这篇文章中我们讲到了用Scroller来实现View的滑动,所以这篇文章我们就不介绍Scroller是如何使用的了,本篇就从源码来分析下S ...
- [iOS] KVC 和 KVO
开发iOS经常会看见KVO和KVC这两个概念,特地了解了一下. 我的新博客wossoneri.com link KVC Key Value Coding KVC是一种用间接方式访问类的属性的机制.比如 ...