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 ...
随机推荐
- 报错 IllegalArgumentException occurred calling getter of cn.itcast.entity.Customer.cid
我碰到这个问题的时候,没有数据类型不匹配的情况,也没有表达无法向action传递数据的情况,一直报这样的错误,意思就是无法使用Customer中的get方法来赋值.完整报错如下所示: HTTP Sta ...
- HTML5 ——web audio API 音乐可视化(一)
使用Web Audio API可以对音频进行分析和操作,最终实现一个音频可视化程序. 最终效果请戳这里; 完整版代码请戳这里,如果还看得过眼,请给一个start⭐ 一.API AudioContext ...
- linux find命令使用(转)
常用命令 find (目录) [-type d | f] (文件夹 | 文件) -name (名称,可使用正则表达式) find /root -name "*core&q ...
- SpringBoot下的Dubbo和Zookeeper整合
最近一直在学各种分布式的内容,学到了dubbo分布式服务框架就想写个小demo使用一下,但是由于我要整合在SpringBoot框架中,SpringBoot框架毕竟提倡的是java注解配置,XML文件不 ...
- 用OpenCV进行视频截取
记录用OpenCV进行视频截取. 核心代码如下: CvCapture* capture = cvCreateFileCapture(src_avi_file.c_str()); if (capture ...
- Pro C/C++环境搭建
一. 安装oracle软件或者oracle客户端. 二. 载入orasql10.lib (1)在连接器->常规->附件库目录中,加入orasql10.lib库所在目录. (2)在连接器-& ...
- 条款11:记得在operator=中处理自赋值的情况。
本来的版本是这样的: Widget & Widget::operator=(Widget rhs) { delete pb;//这里可能直接将rhs的pb删除了 pb = new (*rhs. ...
- 2017.11.18 IAP下载(STM8,PIC,STM32)
客户要求用IAP下载,mark一下,客户还给了stm32的引导码.仅供参考. 1 PIC单片机的IAP 2 STm32 IAP https://www.cnblogs.com/WeyneChen/p ...
- 函数声明提升机制在浏览器中的bug
JavaScript 解释器中存在一种变量声明被提升(hoisting)的机制,也就是说变量(函数)的声明会被提升到作用域的最前面,即使写代码的时候是写在最后面,也还是会被提升至最前面. 但通过测试, ...
- 苹果iOS11重磅改版App Store,开发者应该了解这些
苹果在WWDC2017上重磅发布iOS11,其中一项重大更新就是对App Sore的全新改版,我们一起来看看具体有哪些变化,以及对我们会带来哪些影响. App Store的分类变化 在iOS10以前, ...