当前位置:网站首页>Fast finding the number of nodes in a complete binary tree
Fast finding the number of nodes in a complete binary tree
2022-06-09 19:01:00 【GreyZeng】
author :Grey
Original address : Quickly find the number of nodes of a complete binary tree
Topic link
LeetCode 222. The number of nodes in a complete binary tree
Title Advanced requirements
** Advanced :** Traversing the tree to count nodes is a method with time complexity of
O(n)A simple solution . Can you design a faster algorithm ?
Violence solution
This property of complete binary tree is not considered , Traverse the binary tree directly , Collect the number of nodes of the left and right subtrees , Then add the header node , Is the number of nodes in the complete binary tree , The complete code is as follows
public static int countNodes1(TreeNode head) {
if (head == null) {
return 0;
}
return p(head).all;
}
public static Info p(TreeNode head) {
if (head == null) {
return new Info(0);
}
Info leftInfo = p(head.left);
Info rightInfo = p(head.right);
// Collect the number of left and right subtree nodes plus the head node , Is the number of nodes in the whole tree
int all = leftInfo.all + rightInfo.all + 1;
return new Info(all);
}
public static class Info {
public int all;
public Info(int a) {
all = a;
}
}
The time complexity is O(N).
Optimal solution
We need to use the properties of a complete binary tree , First , We know , The height of the tree , depth , The hierarchy has the following relationship :

For a complete binary tree , The head node slides to the left until the number of layers obtained by the leaf node must be the maximum number of layers of the binary tree , Suppose the value is h.
If the layer number of the leftmost node of the right tree of the binary tree is exactly equal to h, It shows that the left tree of this binary tree must be a full binary tree

If the layer number of the leftmost node of the right tree of the binary tree is not equal to h, The right tree of this binary tree must be a full binary tree

Because the number of nodes in a full binary tree can be calculated from the height of the tree :
public static int countNodes(TreeNode head) {
if (head == null) {
return 0;
}
int h = maxLenOfLeft(head, 1);
return count(head, 1, h);
}
private static int count(TreeNode head, int level, int h) {
if (level == h) {
return 1;
}
if (maxLenOfLeft(head.right, level + 1) == h) {
// The left tree must be full
return (1 << (h - level)) + count(head.right, level + 1, h);
} else {
// The right number must be full , Note that the height is h - level - 1
return (1 << (h - level - 1)) + count(head.left, level + 1, h);
}
}
public static int maxLenOfLeft(TreeNode root, int level) {
while (root != null) {
root = root.left;
level++;
}
return level - 1;
}
By the above method , We don't have to traverse the entire binary tree , You can calculate the nodes , The complexity is lower than O(N).
more
边栏推荐
- Why can't the redis module be referenced in the function method called by the foreachpartition method of DF?
- 联发科:市场需求不会消失,未来3年营收年复合增长率将超14%
- Win10 installs wsl1 on disks D, e and F
- How to use wireless communication technology to optimize the fire water pipe network of iron and steel plant?
- Stlink-v2-1 burning to cmsis-dap
- Ad PCB drawing transparency
- Investment of 550million yuan! Merck announced to build an advanced semiconductor integration base in Zhangjiagang, China
- 有源差分探头在USB2.0一致性分析测试的准备工作
- WSL attaching USB flash disk
- 测量电流探头如何降低噪音
猜你喜欢

第一次畫板子小記

可视化展示炫酷3D图表

Audio 3A processing practice makes your application more "pleasant"

前美联储高级经济学家胡捷:从USDD升级看未来金融趋势

How to realize wireless monitoring of factory production energy consumption data?

精益产品开发体系最佳实践及原则

明细表1字符串拼接合并插入到明细表2SQL输出过程记录

Introduction to Multivariate Statistics

How to realize face verification quickly and accurately?

What software is foreign exchange transaction MT4? What is the difference between MT4 and MT5? What should I pay attention to when downloading MT4?
随机推荐
快速求完全二叉树的节点个数
【数据库数据恢复】windows server环境下SqlServer数据库文件未知原因丢失的数据恢复案例
视觉AI芯片厂商肇观电子获亿元融资,工银资本领投
DL|循环神经网络部分
开关损耗测试方案中的示波器探头应用
金鱼哥RHCA回忆录:DO447管理清单--章节实验
leetcode:剑指 Offer 56 - I. 数组中数字出现的次数【分组异或】
SQL第四练:字符串处理函数
一文彻底理解并发编程中非常重要的票据锁——StampedLock
How to improve the click through rate of push messages through a/b testing?
Notes sur le premier tableau
国联期货开户安全吗?国联期货公司开户方法是什么?
What is a cluster? Why use a cluster architecture?
DBeaver中如何调整SQL编辑器的字体大小
IEDA 安装actiBPM插件
评“开发人员不喜欢低代码和无代码的8个理由”
155_模型_Power BI & Power Pivot 进销存之安全库存
Schedule 1 string splicing and inserting into schedule 2sql output process record
How to set up wireless host link communication between Kingview and OMRON PLC?
Structure design of high pressure differential probe