当前位置:网站首页>222. 完全二叉树的节点个数
222. 完全二叉树的节点个数
2022-07-27 02:19:00 【小卢要刷力扣题】
222. 完全二叉树的节点个数
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2h 个节点。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/count-complete-tree-nodes
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
解题思路

完全二叉树的高度怎么求?一直往左看,看最深到第几层就是完全二叉树的高度
首先数一下一共有几层,然后找右数上的最左节点如果没有达到最大深度5层,现在只有4层说明
右树是满的,高度是3

因为我们永远有总层数,如果总层数10层,
如果当前节点在第5层,如果发现右树上的最左节点没到第10层,到第9层
说明右树是满的,高度是4
右树节点个数为24

如果总层数10层,右树上的最左节点到了第10层,说明左树是满的,高度是5
永远在找头节点右树上的最左
它如果到了最后一层,左边直接公式求出来了
它如果没到最后一层, 右边一个公式求出来,之后走左边递归。
永远都看右树最左决定你走左还是走右
代码
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;
}
}
边栏推荐
- Add support for @data add-on in idea
- Debug mode in pycharm for detailed debugging
- Use websocket to realize a web version of chat room (fishing is more hidden)
- C语言力扣第43题之字符串相乘。优化竖式
- 477-82(236、61、47、74、240、93)
- Method of converting curtain article OPML into markdown
- Contour detection based on OpenCV (1)
- Reading notes of Kazuo Inamori's advice to young people
- connman介绍
- Daffodils (day 78)
猜你喜欢

MySQL has a nonexistent error

Duplicate disc: what are the basic attributes of an image? What do you know about images? What are the parameters of the image

若依的环境的部署以及系统的运行

Smart pointer shared_ ptr、unique_ ptr、weak_ ptr

Plato farm brings a new experience to community users through the LAAS protocol elephant swap

redis入门练习

基于OpenCV的轮廓检测(2)

Worthington papain dissociation system solution

FastBoot brush machine

mysql出现不存在错误
随机推荐
The new version of Alibaba Seata finally solves the idempotence, suspension and empty rollback problems of TCC mode
Banyan data model of Bairong
Design method and test method of APP interface use case
Take you to know what Web3.0 is
次轮Okaleido Tiger即将登录Binance NFT,引发社区热议
Ming min investment Qiu Huiming: behind the long-term excellence and excess, the test is the team's investment and research ability and the integrity of strategy
MySQL的数据库有关操作
Plato farm brings a new experience to community users through the LAAS protocol elephant swap
Implementation of API short message gateway based on golang
如何进行 360 评估
Connman introduction
【树链剖分】2022杭电多校2 1001 Static Query on Tree
04.在谷歌浏览器中安装模拟浏览器ChromeDriver的详细步骤
Deeply understand the underlying data structure and algorithm of MySQL index
Basic concept and essence of Architecture
Method of converting curtain article OPML into markdown
redis秒杀案例,跟着b站尚硅谷老师学习
Smart pointer shared_ ptr、unique_ ptr、weak_ ptr
connman介绍
[tree chain dissection] 2022 Hangzhou Electric Multi school 21001 static query on tree