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 ...
随机推荐
- Installing Fonts programatically C#
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...
- [HTML/CSS]有一种节点叫做文本节点
HTML可以看成是由节点(node)组成的树结构 我们一般都是在<p>节点里面写字符串. 在上图中,<p>节点和字符串之间有一个text, 这个text就是文本节点. 我们可以 ...
- Mysql数据库多表查询
一.介绍 首先说一下,我们写项目一般都会建一个数据库,那数据库里面是不是存了好多张表啊,不可能把所有的数据都放到一张表里面,肯定要分表来存数据,这样节省空间,数据的组织结构更清晰,解耦和程度更高,但是 ...
- js控制两个元素高度保持一致
<script type="text/javascript"> $(function(){ if($("#left").height() > ...
- 【代码笔记】Web-ionic-列表
一,效果图. 二,index.html代码. <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- Maven 环境搭建及使用(win10)
最近由于公司项目需要,学习了一下Maven 环境的配置.这里把配置步骤和简单的操作做一个汇总. 一.Maven环境的搭建 1.配置java环境(这里不详述过程,可参考:http://www.cnblo ...
- Angular基础(三) TypeScript
一.模仿Reddit a) 运行ng new –ng4angular-reddit创建应用,从随书代码中复制样式文件,新建组件app-root,代码为: 界面可以看到了: b) 对于界面输入的数据,获 ...
- SDK Manager
dx.bat :将所有的.class文件变成一个.dex文件. aapt:Android Application package tools 安卓应用的打包工具. adb:Android Debug ...
- Android基础之内容提供者的实现
内容提供者可以实现应用间查询数据库的需求 一.在提供数据库访问的应用设置内容提供者 public class AccountProvider extends ContentProvider { sta ...
- 11.1、socket连接中的粘包、精确传输问题
粘包: 发生原因: 当调用send的时候,数据并不是即时发给客户端的.而是放到了系统的socket发送缓冲区里,等缓冲区满了.或者数据等待超时了,数据才会发送,所以有时候发送太快的话,前一份数据还没有 ...