当前位置:网站首页>快速求完全二叉树的节点个数
快速求完全二叉树的节点个数
2022-06-09 18:52:00 【GreyZeng】
作者:Grey
原文地址:快速求完全二叉树的节点个数
题目链接
题目进阶要求
**进阶:**遍历树来统计节点是一种时间复杂度为
O(n)的简单解决方案。你可以设计一个更快的算法吗?
暴力解法
不考虑完全二叉树的这个性质,直接遍历一下二叉树,收集一下左右子树的节点个数,然后加上头节点,就是整个完全二叉树的节点个数,完整代码如下
public static int countNodes1(TreeNode head) {
if (head == null) {
return 0;
}
return p(head).all;
}
public static Info p(TreeNode head) {
if (head == null) {
return new Info(0);
}
Info leftInfo = p(head.left);
Info rightInfo = p(head.right);
// 收集左右子树节点个数加上头节点,就是整棵树的节点个数
int all = leftInfo.all + rightInfo.all + 1;
return new Info(all);
}
public static class Info {
public int all;
public Info(int a) {
all = a;
}
}
时间复杂度是O(N)。
最优解
需要利用一下完全二叉树的性质,首先,我们知道,树的高度,深度,层次有如下关系:

对于一棵完全二叉树,头节点一直往左滑直到叶子节点得到的层数一定是这个二叉树的最大层数,假设这个值为h。
如果这个二叉树右树的最左节点的层数正好等于h,说明这个二叉树左树一定是满二叉树

如果这个二叉树右树的最左节点的层数不等于h,则说明这个二叉树的右树一定是满二叉树

由于满二叉树的节点个数可以通过树的高度计算出来:
public static int countNodes(TreeNode head) {
if (head == null) {
return 0;
}
int h = maxLenOfLeft(head, 1);
return count(head, 1, h);
}
private static int count(TreeNode head, int level, int h) {
if (level == h) {
return 1;
}
if (maxLenOfLeft(head.right, level + 1) == h) {
// 左树一定是满的
return (1 << (h - level)) + count(head.right, level + 1, h);
} else {
// 右数一定是满的,注意高度是 h - level - 1
return (1 << (h - level - 1)) + count(head.left, level + 1, h);
}
}
public static int maxLenOfLeft(TreeNode root, int level) {
while (root != null) {
root = root.left;
level++;
}
return level - 1;
}
通过如上方法,我们无须遍历整个二叉树,就可以把节点计算出来,复杂度低于O(N)。
更多
边栏推荐
- Stlink-v2-1 burning to cmsis-dap
- 氪星晚报丨苹果胜诉,美法官驳回iPhone和iPad安全缺陷集体诉讼;消息称京东将试点餐饮外卖业务
- 20220529-mks格式的字幕怎么转换成srt、ass、ssa、idx格式的字幕.txt
- 一些我平常用到的软件
- How to set up wireless host link communication between Kingview and OMRON PLC?
- Talk about MQ technology selection
- 美国商务部长:考虑将更多中企加入“黑名单”,近期不会放松制裁
- How to improve the click through rate of push messages through a/b testing?
- C# 32. Select a network card from the static class implementation drop-down box
- This article explains in detail three ways for PHP to connect to MySQL database
猜你喜欢

Demagnetization and balance adjustment steps of oscilloscope current probe

Introduction to Multivariate Statistics

3 个注解,优雅的实现微服务鉴权

Alibaba open source TTL is used in microservices to gracefully realize inter thread reuse of identity information

Pro 后台子管理员 403 问题分析

【PB03F 环境搭建】蓝牙5.2安信可PB-03F-Kit开发板二次开发环境搭建

How to measure differential signal with high voltage differential probe

尽一份孝心,为家人做一个老人防摔报警系统

一文彻底理解并发编程中非常重要的票据锁——StampedLock

Why is cloud native data Lake worth paying attention to?
随机推荐
一季度全球半导体设备出货金额达247亿美元,同比增长5%
上位机开发(开篇)
Principle and implementation of avoiding group panic and load balancing
[SolidWorks detailed records] solve the problem of invalid default templates and add custom templates
mfc连接数据库显示未发现数据源名称并且未指定默认驱动程序
What software is foreign exchange transaction MT4? What is the difference between MT4 and MT5? What should I pay attention to when downloading MT4?
Loop structure programming 2
明细表1字符串拼接合并插入到明细表2SQL输出过程记录
Stlink-v2-1 burning to cmsis-dap
WSL attaching USB flash disk
Database: data field change under high-speed parallel distribution
Inspur cs5280h raid scheme details
有源差分探头在USB2.0一致性分析测试的准备工作
220609-R
Win10 installs wsl1 on disks D, e and F
一文详解PHP连接MySQL数据库的三种方式
Pcap 文件
Using fingerprint for windows two factor identity authentication is both safe and convenient
How to realize wireless monitoring of factory production energy consumption data?
氪星晚报丨苹果胜诉,美法官驳回iPhone和iPad安全缺陷集体诉讼;消息称京东将试点餐饮外卖业务