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 ...
随机推荐
- 【原】通过BeanNameAutoProxyCreator改变臃肿代码
前言: 最近接手了一个项目,大概过了下需求,然后打开项目准备开搞的时候发现一个问题,这个项目是提供rest服务的一个web项目,其中很多旧系统由于还没改成微服务,所以只能通过HttpClient发起调 ...
- 如何在表单中使用Ajax
1.HTML就是一个简单表单验证,有登录按钮,点击登录会发送Ajax, 这里就是简单如果用户名为:zhouzhiruo,密码为:123456,就是登录成功,否则登录失败 应该在发送请求之前对input ...
- C#设计模式--迭代器模式(学习Learning hard设计模式笔记)
/// <summary> /// 抽象聚合接口 /// </summary> public interface IListCollection { Iterator GetI ...
- domOperation.js
// 可视宽高var ch = document.documentElement.clientHeightvar cw = document.documentElement.clientWidth / ...
- Centos 7.x 安装 Docker-ce
Centos 下安装 Docker-ce CentOS 7.0, CentOS 7.2: cat > /etc/yum.repos.d/docker-main.repo << -'E ...
- js识别设备
console.log(window.navigator); Navigator 对象属性 appCodeName 返回浏览器的代码名. appMinorVer ...
- python之递归与二分法
1. 递归 自己调用自己 递归的入口(参数) 和 出口(return) 树形结构的遍历 import os def func(lujing, n): lst = os.listdir(lujing) ...
- vue 父子组件互相传值容易出现的报错
对于父子组件之间的互相传值,报错如下: [Vue warn]: Avoid mutating a prop directly since the value will be overwritten w ...
- GIS性能策略
当一个地理平台上线运行,我们经常会遇到这些问题:1.系统刚上线时速度较快,一段时间后访问较慢?2.在地理平台目前的配置下,发布多少个服务才合理?一个服务配置多少个实例数才合适?这些问题,都涉及整个地理 ...
- linux定时任务调度定系统——opencron
linux定时任务调度定系统——opencron https://gitee.com/terrytan/opencron/#%E8%BF%90%E8%A1%8C%E7%8E%AF%E5%A2%83 一 ...