当前位置:网站首页>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
边栏推荐
- LabVIEW continuous sampling and limited sampling mode
- Deep parsing of kubernetes controller runtime
- Esxi based black Qunhui DSM 7.0.1 installation of VMware Tools
- internship:业务流程初识
- 什么是过孔式导电滑环?
- 吸顶方案1
- Taro---day1---搭建项目
- Introduction to memory model of JVM
- IIC communication protocol for single chip microcomputer
- 【DNS 解析】将Name.com的域名接入DNSPod解析
猜你喜欢

Taro---day1---搭建项目

Introduction to memory model of JVM

Proe/creo product structure design - continuous research

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

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

联想拯救者R720如何组建双通道内存

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

Redis configuration and optimization of NoSQL

Ten MySQL locks, one article will give you full analysis

Proe/Creo产品结构设计-钻研不断
随机推荐
Taro---day1---搭建项目
Modern programming language: rust
网页鼠标点击特效案例收集(直播间红心同理)
免费、好用、强大的开源笔记软件综合评测
1696D. Permutation graph thinking
Ceiling scheme 1
#795 Div.2 E. Number of Groups set *
Summary of attack methods of attack team
Why are cloud vendors targeting this KPI?
联想拯救者R720如何组建双通道内存
Informatics Olympiad all in one 1359: enclosed area
AI+临床试验患者招募|Massive Bio完成900万美元A轮融资
Alchemy (2): why use issue management software
Is it safe to open a stock account online now? Select a listed securities firm, and the fastest time to open an account is 8 minutes
股票投资交流群安全吗?入群免费开户靠谱嘛?
打新债注册账户安全吗,会有风险吗?
Matlb| improved forward push back method for solving power flow of low voltage distribution network
Is it safe to open an account online now? Novice is just on the road, ask for the answer
从小到大为何一谈学习就愁眉苦脸
Modern programming languages: zig