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 ...
随机推荐
- hermite插值
Hermite 插值就是要求插值函数不仅经过所给节点,而且要保证在该点的导数也相等.<备注:虽然还不理解这句话,但是还是先放这里!> 所谓样条曲线(Spline Curves)是指给定一组 ...
- appium自动化测试(二)
一. 获取应用包名和入口activity 获取应用包名和入口activity:aapt命令 aapt目录: 安卓sdk的build-tools目录下(如果要在cmd里直接运行,要配置环境变量,否则需要 ...
- 修改当前启动菜单项的HyperVisorLaunchType
switch-hyperv.bat @echo off "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\ ...
- SpringBoot下的Dubbo和Zookeeper整合
最近一直在学各种分布式的内容,学到了dubbo分布式服务框架就想写个小demo使用一下,但是由于我要整合在SpringBoot框架中,SpringBoot框架毕竟提倡的是java注解配置,XML文件不 ...
- Python 用Redis简单实现分布式爬虫
Redis通常被认为是一种持久化的存储器关键字-值型存储,可以用于几台机子之间的数据共享平台. 连接数据库 注意:假设现有几台在同一局域网内的机器分别为Master和几个Slaver Master连接 ...
- nyoj-5-kmp裸题
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=5 kmp统计匹配串出现次数,贼尴尬好久没做字符串题目,一开始求得是文本串的next ...
- 转:session和cookie以及catch三者的区别
以前实现数据的缓存有很多种方法,有客户端的Cookie,有服务器端的Session和Application. 其中Cookie是保存在客户端的一组数据,主要用来保存用户名等个人信息. Session则 ...
- Qt WebKit 学习的说明
(转自:http://it.100xuexi.com/view/otdetail/20120827/4021c662-b917-44d9-8284-910cac713c23.html) QT Webk ...
- ajax用json实现数据传输
JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.它基于ECMAScript的一个子集. JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族 ...
- Week06《Java程序设计》第六次作业总结
Week06<Java程序设计>第六次作业总结 1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图或相关笔记,对面向对象思想进行 ...