当前位置:网站首页>The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]
The number of nodes of a complete binary tree [non-O (n) solution > Abstract dichotomy]
2022-06-28 01:20:00 【REN_ Linsen】
Abstract dichotomy
Preface
Abstract dichotomy is everywhere , To quickly cut down half the useless data , This reduces the time complexity .
Abstract the rules of dichotomy , Then bisection can be flexibly applied to a variety of scenarios .
With O(logN * logN) < O(N) To find the number of nodes of a complete binary tree .
One 、 The number of nodes in a complete binary tree

Two 、 Abstract dichotomy
package everyday.medium;
// The number of nodes in a complete binary tree .
public class CountNodes {
/* target: Find the number of complete binary tree nodes , At most one level of a complete binary tree is not full ; If the last floor is not full , There are also nodes from left to right , And each sub tree has left children first and then right children . key problem : Find the last node of the last layer . Every time a leaf node is found logN, Find in two logN, So time complexity O(logN * logN) < O(N), because O(logN) < O(sqrt(N)) */
public int countNodes(TreeNode root) {
if (root == null) return 0;
// Determine tree height .
int hLeft = getLevel(root);
// Two points
// int width = (int) Math.pow(2, hLeft - 1);
// Read other people's comments , in the light of 2 To what power , Can be replaced by bit operation .
int width = 1 << hLeft - 1;
int low = 0, high = width - 1;
while (low < high) {
// coordination low = mid, Prevent a dead cycle .
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;
}
}
// Find the last node .
if (p == null) high = mid - 1;
else low = mid;
}
// Calculate the total number of nodes . Remove the number of nodes in the last layer + Number of nodes in the last layer
return (int) ((1 << hLeft - 1) - 1) + (low + 1);
}
// Determine the left depth of the tree .
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;
}
}
}
summary
1) Perfect binary tree .
2) With O(logN * logN) To get the number of nodes of a complete binary tree .
3) Abstract dichotomy + How to use O(logN) Find the second of the complete binary tree N A leaf node .
reference
边栏推荐
- Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
- Matlb| optimal configuration of microgrid in distribution system based on complex network
- 最新MySQL高级SQL语句大全
- Acwing第 57 场周赛【未完结】
- plot_model报错:没有安装pydot, graphviz
- Zhang Fan: the attribution of flying pig after advertising based on causal inference technology
- Distortion model of SDF learning
- Squid代理服务器(缓存加速之Web缓存层)
- 哪个证券公司炒股开户是比较好,更安全靠谱的
- 券商买股票用什么app是比较好的,比较安全的
猜你喜欢

Leetcode 720. The longest word in the dictionary

MySQL 18: execution of write statements

How to add live chat in your Shopify store?

Arduino uno realizes simple touch switch through direct detection of capacitance

N methods of data De duplication using SQL
![[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

Comprehensive evaluation of free, easy-to-use and powerful open source note taking software

Proe/Creo产品结构设计-钻研不断

The limits of Technology (11): interesting programming

Understand the basic structure of wechat applet project
随机推荐
What are the requirements for customizing the slip ring for UAV
Class文件结构和字节码指令集
plot_ Model error: pydot and graphviz are not installed
网页鼠标点击特效案例收集(直播间红心同理)
【无标题】
完全二叉树的节点个数[非O(n)求法 -> 抽象二分]
信息学奥赛一本通 1359:围成面积
【开源】开源系统整理-考试问卷等
Leetcode 720. 词典中最长的单词(为啥感觉这道题很难?)
PostgreSQL设置自增字段
独立站卖家都在用的五大电子邮件营销技巧,你知道吗?
lefse分析本地实现方法带全部安装文件和所有细节,保证成功。
MySQL十种锁,一篇文章带你全解析
Flutter SliverAppBar全解析,你要的效果都在这了!
现在网上开股票账户安全吗?选择上市券商,最快8分钟开户成功
Overview and construction of redis master-slave replication, sentinel mode and cluster
GFS 分布式文件系统概述与部署
#795 Div.2 D. Max GEQ Sum 单调栈
Technical debt wall: a way to make technical debt visible and negotiable
Taro--- day2--- compile and run