当前位置:网站首页>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;
}
}
边栏推荐
- connman介绍
- 基于OpenCV的轮廓检测(2)
- Briefly sort out the dualpivotquicksort
- 九方智投是正规公司吗?一起聊聊九方智投
- Okaleido tiger is about to log in to binance NFT in the second round, which has aroused heated discussion in the community
- Introduction to database - a brief introduction to MySQL
- [tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree
- J-3-point practice in the second game of 2022 Niuke multi school
- 数字孪生实际应用:智慧城市项目建设解决方案
- 【无标题】JDBC连接数据库读超时
猜你喜欢

飞腾腾锐 D2000 荣获数字中国“十大硬核科技”奖

Learning and understanding of four special data types of redis

Characteristics and determination scheme of Worthington pectinase

FastBoot brush machine

Principle understanding and application of hash table and consistent hash

MySQL Chinese failure

Explain tool actual operation

Deployment of ruoyi's environment and operation of the system
![[Android synopsis] kotlin multithreaded programming (I)](/img/04/4349bacbd401868d73a3b05d018b66.png)
[Android synopsis] kotlin multithreaded programming (I)

关于使用hyperbeach出现/bin/sh: 1: packr2: not found的解决方案
随机推荐
Practical application of digital twins: smart city project construction solution
Textbox in easyUI inserts content at the cursor position
flinkSQLclient创建的job,flink重启就没了,有什么办法吗?
Characteristics and experimental suggestions of abbkine abfluor 488 cell apoptosis detection kit
Principle understanding and application of hash table and consistent hash
Detailed tutorial of typera
477-82(236、61、47、74、240、93)
FastBoot brush machine
Meta Quest内容生态总监谈App Lab设计初衷
Feitengtengrui d2000 won the "top ten hard core technologies" award of Digital China
Binary tree (day 82)
MySQL underlying data structure
LeetCode 第二十七天
Is Jiufang intelligent investment a regular company? Talk about Jiufang intelligent investment
Debug mode in pycharm for detailed debugging
Vector to SVG method
Prime factorization -- C (GCC) -- PTA
基于OpenCV的轮廓检测(1)
Introduction to redis
ZJCTF_ login