当前位置:网站首页>完全二叉树的节点个数[非O(n)求法 -> 抽象二分]
完全二叉树的节点个数[非O(n)求法 -> 抽象二分]
2022-06-27 22:57:00 【REN_林森】
前言
抽象二分无处不在,以快速砍掉一半无用数据,从而降低时间复杂度。
将二分判定的规则抽象,则二分可灵活的应用到多种场景种。
以 O(logN * logN) < O(N) 的低时间复杂度来求完全二叉树的节点个数。
一、完全二叉树的节点个数

二、抽象二分
package everyday.medium;
// 完全二叉树的节点个数。
public class CountNodes {
/* target:寻找完全二叉树节点个数, 完全二叉树最多有一层没满;最后一层若是没满,也是从左到右有节点,并且每棵子树先左孩子再右孩子。 关键问题:二分找到最后一层的最后一个节点。 每次找到叶子节点logN,以二分的方式找logN,所以时间复杂度O(logN * logN) < O(N),因为O(logN) < O(sqrt(N)) */
public int countNodes(TreeNode root) {
if (root == null) return 0;
// 确定树高。
int hLeft = getLevel(root);
// 二分
// int width = (int) Math.pow(2, hLeft - 1);
// 看了别人的评论,针对2的多少次方,可用位运算替代。
int width = 1 << hLeft - 1;
int low = 0, high = width - 1;
while (low < high) {
// 配合low = mid,防止死循环。
int mid = low + (high - low + 1 >>> 1);
int level = hLeft;
int sum = 0;
TreeNode p = root;
while (level-- > 1) {
if ((sum + (1 << level - 1)) > mid) p = p.left;
else {
sum += 1 << level - 1;
p = p.right;
}
}
// 寻找最后一个节点。
if (p == null) high = mid - 1;
else low = mid;
}
// 计算总节点数。去掉最后一层的节点数 + 最后一层的节点数
return (int) ((1 << hLeft - 1) - 1) + (low + 1);
}
// 确定树左深度。
private int getLevel(TreeNode root) {
int h = 0;
while (root != null) {
++h;
root = root.left;
}
return h;
}
// Definition for a binary tree node.
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
}
总结
1)完全二叉树。
2)以 O(logN * logN) 的方式来得到完全二叉树的节点数。
3)抽象二分 + 如何以 O(logN) 找到完全二叉树的第 N 个叶子节点。
参考文献
边栏推荐
- Acwing game 57 [unfinished]
- Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
- 快速掌握grep命令及正则表达式
- What are cookies and the security risks of v-htm
- From small to large, why do you always frown when talking about learning
- 796 div.2 C. managing history thinking
- Quickly master grep commands and regular expressions
- . Mp4 video test address
- Matlb| improved forward push back method for solving power flow of low voltage distribution network
- Sword finger offer 61 Shunzi in playing cards
猜你喜欢

Summary of wuenda's machine learning course (14)_ Dimensionality reduction

单晶炉导电滑环的应用范围和作用是什么

Introduction to data warehouse

Class文件结构和字节码指令集

MATLB|基于复杂网络的配电系统微电网优化配置

How to add live chat in your Shopify store?

The flutter slivereappbar is fully parsed. All the effects you want are here!

logging日志的使用

Why are cloud vendors targeting this KPI?

为什么要选择不锈钢旋转接头
随机推荐
Alchemy (3): how to do a good job in interfacing a business process
golang 猴子吃桃子,求第一天桃子的数量
.mp4视频测试地址
给女朋友看的消息中间件
Differences and functions between intranet IP and public IP
IIC communication protocol for single chip microcomputer
Overview and deployment of GFS distributed file system
打新债注册账户安全吗,会有风险吗?
CharSequence初探
zotero文献管理工具安装使用
Is it safe to open an account for mobile stocks? Where can I open an account for buying stocks?
Startup and shutdown of Oracle Database
GFS 分布式文件系统概述与部署
796 div.2 C. managing history thinking
RNA SEQ introduction practice (I): upstream data download, format conversion and quality control cleaning
哪个证券公司炒股开户是比较好,更安全靠谱的
Alchemy (2): why use issue management software
供应链高效管理供应商
Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
Ceiling scheme 1