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 ...
随机推荐
- server_2003_r2_standard_sp2_vl_X13-46532
1. 安装的是 cn_win_srv_2003_r2_standard_with_sp2_vl_cd1_X13-46532.iso CD2 它没有要求装 也就没装,貌似 网上搜到 安装CD2需要另外的 ...
- scala学习手记25 - Curry化
curry翻译为中文就是咖喱.意为使用curry可以让代码更有味道. scala里的curry化可以把函数从接收多个参数转换成接收多个参数列表.也就是说我们要编写的函数不是只有一个参数列表,这个参数列 ...
- scala学习手记18 - Any和Nothing
Any 前面已经有两次提到过:在scala中,Any类是所有类的超类. Any有两个子类:AnyVal和AnyRef.对应Java直接类型的scala封装类,如Int.Double等,AnyVal是它 ...
- python脚本1_给一个半径求圆的面积和周长
#给一个半径,求圆的面积和周长,圆周率3.14 r = int(input('r=')) print('area='+str(3.14*r*r)) print('circumference='+str ...
- RabbitMQ Consumer获取消息的两种方式(poll,subscribe)解析
以下转自:http://blog.csdn.net/yangbutao/article/details/10395599 rabbitMQ中consumer通过建立到queue的连接,创建channe ...
- 51nod 1161 组合数,规律
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1161 显然,题目可以转化为矩阵求解,但复杂度显然时空都不允许,我们如果自 ...
- IOS-真机相关
真机调试 Certificates 证书 Identifiers 标示符 Profiles 描述文件 一. 证书,安装在电脑上,只有安装了证书的电脑,才有可能进行真机调试. - All - De ...
- ASP.NET MVC性能优化(实际项目中)
前言 在开发中为了紧赶项目进度而未去关注性能的问题,在项目逐渐稳定下来后发现性能令人感到有点忧伤,于是开始去关注这方面,本篇为记录在开发中遇到的问题并解决,不喜勿喷.注意:以下问题都是在移动端上出现, ...
- Java基础学习-extends继承(成员变量,局部变量,成员方法)
package extend; /*面向对象-继承: * 多个类的共同成员变量和成员方法.抽取到另一个类中(父类),我们多个类就可以访问到父类的成员了 * */ class Game{ String ...
- mysql的5.6版本支持分区吗?
转载请注明出处:http://blog.csdn.net/dongdong9223/article/details/72291698 本文出自[我是干勾鱼的博客] 我们知道,查看mysql是否支持分区 ...