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 ...
随机推荐
- selenium学习笔记(selenium IDE下载安装)
今天自己一直在瞎捣鼓 最后这里整理下 selenium IDE 这个录制工具的下载安装 首先这个工具只支持火狐浏览器firefox.使用火狐浏览器进入selenium官网: http://www.se ...
- linux中的kill命令
一. 定义 kill命令用来删除执行中的程序或工作.kill可将指定的信息送至程序.预设的信息为SIGTERM(15),可将指定程序终止.若仍无法终止该程序,可使用SIGKILL(9)信息尝试强制删除 ...
- Ceph配置项动态变更机制浅析
转自:https://www.ustack.com/blog/ceph%e9%85%8d%e7%bd%ae%e9%a1%b9%e5%8a%a8%e6%80%81%e5%8f%98%e6%9b%b4%e ...
- iOS自动化探索(五)自动化测试框架pytest - Assert断言的使用
使用assert语句进行断言 pytest允许使用标准的python assert语法,用来校验expectation and value是否一致 代码演示: def func(): def test ...
- 释伴:Linux 上的 Shebang 符号(#!)
使用Linux或者unix系统的同学可能都对#!这个符号并不陌生,但是你真的了解它吗? 本文了将给你简单介绍一下Shebang(”#!”)这个符号. 首先,这个符号(#!)的名称,叫做”Shebang ...
- <input type="checkbox" name="" value=" ">
<input type="checkbox" name="" value=" ">
- Visual Studio 2008常见问题
1.asp.net在什么软件上运行?学习asp往往需要测试asp程序,电脑不能直接测试,需要装IIS才能运行,但装IIS要么需要安装盘,要么需要安装包,而且设置也很复搜索杂.这里给大学推荐两个替代II ...
- Android的布局方式
1.LinearLayout(线性布局) android:orientation="vertical" //布局 android:layout_width="wrap_c ...
- 剑指Offer(第二版)面试案例:树中两个节点的最低公共祖先节点
(尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74612786冷血之心的博客) 剑指Offer(第二版)面试案例:树 ...
- 使用 event.preventDefault 拦截表单的提交
event.preventDefault() 方法 W3C 官方的定义是:取消事件的默认动作,不单单可以拦截表单的提交,<a>标签的跳转, <input>标签的输入等等默认动作 ...