当前位置:网站首页>222. Number of nodes of complete binary tree
222. Number of nodes of complete binary tree
2022-07-27 03:57:00 【Xiao Lu wants to brush the questions】
List of articles
222. The number of nodes in a complete binary tree
Here's a tree for you Perfect binary tree The root node root , Find out the number of nodes of the tree .
Perfect binary tree Is defined as follows : In a complete binary tree , Except that the lowest node may not be full , The number of nodes in each layer reaches the maximum , And the nodes of the lowest layer are concentrated in the leftmost positions of the layer . If the bottom layer is No h layer , Then the layer contains 1~ 2h Nodes .
source : Power button (LeetCode)
link :https://leetcode.cn/problems/count-complete-tree-nodes
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking

How to find the height of a complete binary tree ? Keep looking to the left , The deepest layer is the height of a complete binary tree
First, count the total number of floors , Then find the leftmost node on the right number if it does not reach the maximum depth 5 layer , Now only 4 Layer description
The right tree is full , Height is 3

Because we always have a total number of floors , If the total number of floors 10 layer ,
If the current node is at 5 layer , If you find that the leftmost node on the right tree does not reach the 10 layer , To the first 9 layer
It shows that the right tree is full , Height is 4
The number of nodes in the right tree is 24

If the total number of floors 10 layer , The leftmost node on the right tree arrives at 10 layer , It shows that the left tree is full , Height is 5
Always find the leftmost tree on the right of the node
If it reaches the last floor , The direct formula on the left is solved
If it doesn't reach the last floor , Find the formula on the right , Then go left recursion .
Always look at the leftmost tree on the right to decide whether you go left or right
Code
class Solution {
public int countNodes(TreeNode root) {
if(root==null){
return 0;
}
return bs(root,1,mostLestLevel(root,1));
}
public int bs(TreeNode root,int level,int h){
if(level==h){
return 1;
}
if(mostLestLevel(root.right,level+1)==h){
return (1<<(h-level))+bs(root.right,level+1,h);
}else{
return (1<<(h-level-1))+bs(root.left,level+1,h);
}
}
public int mostLestLevel(TreeNode node,int level){
while(node!=null){
level++;
node=node.left;
}
return level-1;
}
}
边栏推荐
- Do you really understand code rollback?
- 数字孪生实际应用:智慧城市项目建设解决方案
- C language force deduction question 43 string multiplication. Optimized vertical
- 关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案
- Characteristics and determination scheme of Worthington pectinase
- [regular] judgment, mobile number, ID number
- Plato Farm有望通过Elephant Swap,进一步向外拓展生态
- C # using sqlsugar updatable system to report invalid numbers, how to solve it? Ask for guidance!
- Implementation of API short message gateway based on golang
- LPCI-252通用型PCI接口CAN卡的功能和应用介绍
猜你喜欢

Connman introduction

Redis spike case, learn from Shang Silicon Valley teacher in station B
![Machine learning [Matplotlib]](/img/d1/31ec2f96ca96465c6863798c7db179.jpg)
Machine learning [Matplotlib]

About the solution of using hyperbeach to appear /bin/sh: 1: packr2: not found

The application and significance of digital twins are the main role and conceptual value of electric power.

Plato Farm有望通过Elephant Swap,进一步向外拓展生态

Process analysis of object creation

connman介绍

The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode

Chapter 4 决策树和随机森林
随机推荐
03.获取网页源代码
一维数组的应用
基于OpenCV的轮廓检测(1)
Introduction to database - a brief introduction to MySQL
LeetCode 第二十七天
Reading notes of Kazuo Inamori's advice to young people
Redis spike case, learn from Shang Silicon Valley teacher in station B
Meta Quest内容生态总监谈App Lab设计初衷
Installation and use of anti-virus software ClamAV
Csu18m91 is used as the master controller of the intelligent scale scheme
477-82(236、61、47、74、240、93)
768. 最多能完成排序的块 II 贪心
OC message mechanism
Prime factorization -- C (GCC) -- PTA
Process analysis of object creation
477-82(236、61、47、74、240、93)
Daffodils (day 78)
[untitled] JDBC connection database read timeout
SkyWalking分布式系统应用程序性能监控工具-中
Interview question: the difference between three instantiated objects in string class