当前位置:网站首页>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
边栏推荐
- What are the requirements for customizing the slip ring for UAV
- 章凡:飞猪基于因果推断技术的广告投后归因
- 795 div.2 D. Max GEQ sum monotone stack
- Download, configuration and installation of MySQL
- 什么是过孔式导电滑环?
- Is the stock investment exchange group safe? Is it reliable to open an account for free?
- Cloud native O & M article plan
- 哪个证券公司炒股开户是比较好,更安全靠谱的
- Ai+ clinical trial patient recruitment | massive bio completed round a financing of $9million
- 1696D. Permutation graph thinking
猜你喜欢

剑指 Offer 61. 扑克牌中的顺子

如何在您的Shopify商店中添加实时聊天功能?

Taro---day2---编译运行

Understand the basic structure of wechat applet project

Modern programming language: rust

去哪儿网(Qunar) DevOps 实践分享

N methods of data De duplication using SQL

Form forms and form elements (input, select, textarea, etc.)

【说明】Jmeter乱码的解决方法

Installation and use of Zotero document management tool
随机推荐
Redis configuration and optimization of NoSQL
The Internet industry has derived new technologies, new models and new types of industries
Download, configuration and installation of MySQL
Acwing第 57 场周赛【未完结】
What is the e-commerce conversion rate so abstract?
Summary of wuenda's machine learning course (14)_ Dimensionality reduction
Distortion model of SDF learning
Message Oriented Middleware for girlfriends
IIC communication protocol for single chip microcomputer
#795 Div.2 E. Number of Groups set *
Alchemy (6): iteratable models and use cases
Introduction to memory model of JVM
1696D. Permutation graph thinking
From small to large, why do you always frown when talking about learning
Acwing game 57 [unfinished]
Deepmind | pre training of molecular property prediction through noise removal
去哪儿网(Qunar) DevOps 实践分享
Why are cloud vendors targeting this KPI?
AI+临床试验患者招募|Massive Bio完成900万美元A轮融资
手机股票开户安全吗,买股票在哪开户?