当前位置:网站首页>完全二叉树的节点个数[非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 个叶子节点。
参考文献
边栏推荐
- 剑指 Offer 65. 不用加减乘除做加法
- How about the market application strength of large-size conductive slip rings
- Deep parsing of kubernetes controller runtime
- Summary of wuenda's machine learning course (11)_ Support vector machine
- Electron window background transparent borderless (can be used to start the page)
- Internship: business process introduction
- #795 Div.2 E. Number of Groups set *
- Class文件结构和字节码指令集
- Logging log usage
- Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
猜你喜欢

Arduino uno realizes simple touch switch through direct detection of capacitance

What is a through-hole conductive slip ring?

Flutter SliverAppBar全解析,你要的效果都在这了!

云厂商为什么都在冲这个KPI?

FB、WhatsApp群发消息在2022年到底有多热门?

The limits of Technology (11): interesting programming

Introduction to data warehouse
![[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science](/img/7b/8b3619d7726fdaa58da46b0c8451a4.png)
[Reading Abstract] what is wrong with English Reading Teaching in schools-- Empiricism and the PK of cognitive science

Matlb| optimal configuration of microgrid in distribution system based on complex network

Squid代理服务器(缓存加速之Web缓存层)
随机推荐
HCIP/HCIE Routing&Switching / Datacom备考宝典系列(十九)PKI知识点全面总结(公钥基础架构)
Is it safe for Xiaobai in the stock market to open an account on the Internet?
单晶炉导电滑环的应用范围和作用是什么
Which securities speculation account opening commission is the cheapest and safest
Alchemy (3): how to do a good job in interfacing a business process
Overview and deployment of GFS distributed file system
剑指 Offer 65. 不用加减乘除做加法
Summary of wuenda's machine learning course (11)_ Support vector machine
Startup and shutdown of Oracle Database
Latest MySQL advanced SQL statement Encyclopedia
golang 猴子吃桃子,求第一天桃子的数量
Why stainless steel swivel
#795 Div.2 E. Number of Groups set *
Why are cloud vendors targeting this KPI?
MySQL十种锁,一篇文章带你全解析
哪个证券炒股开户佣金是最便宜,最安全的
Alchemy (1): identify prototype, demo and MVP in project development
LabVIEW连续采样与有限采样模式
Zhang Fan: the attribution of flying pig after advertising based on causal inference technology
Matlb| optimal configuration of microgrid in distribution system based on complex network