1115 Counting Nodes in a BST
题意:给出一棵二叉搜索树的插入序列,要求该树最后两层的结点个数。
思路:在树结点中增加一个数据域layer,表示该结点所在的层次。另外,设置数组level[]和变量maxLevel,level[i]表示第i层的结点个数,maxLevel表示树的最大层次,在层序遍历时更新即可。
代码:
#include <cstdio>
#include <queue>
using namespace std;
]={};
;
struct Node{
int val;
int layer;
Node *lchild,*rchild;
Node(),lchild(NULL),rchild(NULL){}
};
void insert(Node* &root,int val)
{
if(root==NULL) {
root=new Node(val);
return;
}
if(val<=root->val) insert(root->lchild,val);//根据题意,这里是<=,要仔细!
else insert(root->rchild,val);
}
void levelOrderTraversal(Node* root)
{
queue<Node*> q;
root->layer=;
q.push(root);
while(!q.empty()){
Node* pNode=q.front();
q.pop();
level[pNode->layer]++;
if(pNode->layer > maxLevel) maxLevel=pNode->layer;
if(pNode->lchild){
pNode->lchild->layer=pNode->layer+;
q.push(pNode->lchild);
}
if(pNode->rchild){
pNode->rchild->layer=pNode->layer+;
q.push(pNode->rchild);
}
}
}
int main()
{
int n,v;
scanf("%d",&n);
Node* root=NULL;
while(n--){
scanf("%d",&v);
insert(root,v);
}
levelOrderTraversal(root);
printf(],level[maxLevel-]+level[maxLevel]);
;
}
1115 Counting Nodes in a BST的更多相关文章
- 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 ...
- 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 ...
- [二叉查找树] 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 ...
- PAT 甲级 1115 Counting Nodes in a BST
https://pintia.cn/problem-sets/994805342720868352/problems/994805355987451904 A Binary Search Tree ( ...
- 1115. Counting Nodes in a BST (30)
A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...
- PAT 1115 Counting Nodes in a BST
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甲题题解-1115. Counting Nodes in a BST (30)-(构建二分搜索树+dfs)
题意:给出一个序列,构建二叉搜索树(BST),输出二叉搜索树最后两层的节点个数n1和n2,以及他们的和sum: n1 + n2 = sum 递归建树,然后再dfs求出最大层数,接着再dfs计算出最后两 ...
- PAT A 1115. Counting Nodes in a BST (30)【二叉排序树】
题目:二叉排序树,统计最后两层节点个数 思路:数组格式存储,insert建树,dfs遍历 #include<cstdio> #include<iostream> #includ ...
随机推荐
- TF卡.之前的(20180923)
1.京东上搜到的(购买记录) ZC:64G的应该是 只买了一个 另一个取消了 2.TB上搜到的 购买记录是这样的(注意 也有 取消的) 3. 4. 5.
- HttpClient示例01
1.要使用 HttpClient 需要下载 Apache的相关包 我这里下载的是 httpcomponents-client-4.5.2-bin.zip.httpcomponents-client-4 ...
- css tips —— 神奇的max-width,min-width, width覆盖规则
max-width在比width小时,即使width使用!important来加权,仍会max-width生效: max-width比min-width小时,width < min-width, ...
- python之linecache使用
Python linecache模块缓存读取大文件指定行 linecache模块的作用是将文件内容读取到内存中,进行缓存,而不是每次都要从硬盘中读取,这样效率提高很多,又省去了对硬盘IO控制器的频繁操 ...
- python---迭代器与生成器(一)
迭代器与生成器 迭代器 迭代是Python最强大的功能之一,是访问集合元素的一种方式.. 迭代器是一个可以记住遍历的位置的对象. 迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代 ...
- 卡尔曼滤波——基本假设(1)线性系统(2)高斯分布 根据x(t) 求解x(t+1)
from:https://blog.csdn.net/u010720661/article/details/63253509 原文链接:http://www.bzarg.com/p/how-a-kal ...
- Shiro快速入门
1.什么是Shiro Shiro是Java的一个安全框架, 完成权限控制的任务. 权限控制的基本功能: 认证(让系统知道你是谁); 授权(让系统知道你能做什么)权限控制常用的技术: 过滤器/拦截器, ...
- SQLSERVER store procedure 临时表
有些时候显示重复数据时,使用: ) 但有些时候表A过大或者逻辑复杂.显示数据时,会造成性能的影响,这时你就可以使用临时表: ) create table #temp( XXX , XXX) )in ...
- MongoDB 高可用集群架构简介
在大数据的时代,传统的关系型数据库要能更高的服务必须要解决高并发读写.海量数据高效存储.高可扩展性和高可用性这些难题.不过就是因为这些问题Nosql诞生了. 转载自严澜的博文——<如何搭建高效的 ...
- Android下利用RadioGroup和RadioButton实现Tabbar的效果
本实现方法主要使用RadioGroup和RadioButton的组合方式来实现Tabbar的效果. 其中选中的Tab的切换的动作可以通过RadioGroup的OnCheckedChangeListen ...