当前位置:网站首页>二叉树——222. 完全二叉树的节点个数
二叉树——222. 完全二叉树的节点个数
2022-07-25 23:55:00 【向着百万年薪努力的小赵】
1 题目描述
给你一棵 完全二叉树 的根节点 root ,求出该树的节点个数。
完全二叉树 的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第 h 层,则该层包含 1~ 2^h 个节点。
2 题目示例

示例 2:
输入:root = []
输出:0
示例 3:
输入:root = [1]
输出:1
3 题目提示
树中节点的数目范围是[0, 5 * 104]
0 <= Node.val <= 5 * 104
题目数据保证输入的树是 完全二叉树
4 思路
对于任意二叉树,都可以通过广度优先搜索或深度优先搜索计算节点个数,时间复杂度和空间复杂度都是O(n),其中n是二叉树的节点个数。这道题规定了给出的是完全二叉树,因此可以利用完全二叉树的特性计算节点个数。
规定根节点位于第0层,完全二叉树的最大层数为h。根据完全二叉树的特性可知,完全二叉树的最左边的节点一定位于最底层,因此从根节点出发,每次访问左子节点,直到遇到叶子节点,该叶子节点即为完全二叉树的最左边的节点,经过的路径长度即为最大层数h。
具体做法是,根据节点个数范围的上下界得到当前需要判断的节点个数k,如果第k个节点存在,则节点个数一定大于或等于k,如果第k个节点不存在,则节点个数一定小于k,由此可以将查找的范围缩小—半,直到得到节点个数。
如何判断第k个节点是否存在呢?如果第k个节点位于第h层,则k的二进制表示包含h+1位,其中最高位是1,其余各位从高到低表示从根节点到第k个节点的路径,О表示移动到左子节点,1表示移动到右子节点。通过位运算得到第k个节点对应的路径,判断该路径对应的节点是否存在,即可判断第k个节点是否存在。
5 我的答案
class Solution {
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int level = 0;
TreeNode node = root;
while (node.left != null) {
level++;
node = node.left;
}
int low = 1 << level, high = (1 << (level + 1)) - 1;
while (low < high) {
int mid = (high - low + 1) / 2 + low;
if (exists(root, level, mid)) {
low = mid;
} else {
high = mid - 1;
}
}
return low;
}
public boolean exists(TreeNode root, int level, int k) {
int bits = 1 << (level - 1);
TreeNode node = root;
while (node != null && bits > 0) {
if ((bits & k) == 0) {
node = node.left;
} else {
node = node.right;
}
bits >>= 1;
}
return node != null;
}
}
边栏推荐
猜你喜欢
![[learning notes] solid works operation record](/img/f7/0535b473755643ce32996f00fa5554.png)
[learning notes] solid works operation record

Article 75: writing skills of academic papers

Read the field status of account in ABAP code (hidden, optional, required)

C - readonly and const keywords
![[JUC] concurrent keyword volatile](/img/80/2f1b33f1e8c87fd4f8806eafb83139.png)
[JUC] concurrent keyword volatile

Generating random number random learning uniform_ int_ distribution,uniform_ real_ distribution

如何用yolov5 做个闯红灯监控的智能交通系统(1)

What is inode? It will help you understand inode and what help inode provides when creating and deleting files.

死信队列 和消息TTL过期代码

How to solve cross domain problems
随机推荐
typescript ts 基础知识之类
Payment terms in SAP message No. vg202 IDoc e1edk18 have been transferred: check data
指针函数的demo
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
[learning notes] solid works operation record
Practical experience of pair programming
抽丝剥茧C语言(高阶)程序环境和预处理
Lua script Wireshark plug-in to resolve third-party private protocols
Firewall command simple operation
SIGIR‘22 推荐系统论文之图网络篇
下一代终端安全管理的关键特征与应用趋势
Zhiniu stock -- 09
arcgis根据矢量范围裁取tif影像(栅格数据)、批量合并shp文件、根据矢量范围裁取区域内的矢量,输出地理坐标系、转换16位TIF影像的像素深度至8位、shp文件创建和矢量框标绘设置
Fixed and alternate sequential execution of modes
1223. 掷骰子模拟 范围DP
ABAP 代码中读取会计科目的字段状态(隐藏、可选、必输)
S4/HANA ME21N创建PO 输出控制消息按钮丢失解决方法(切换EDI 输出模式BRF+至NAST模式)
Matchmaker's words
Part 66: monocular 3D reconstruction point cloud
Key features and application trends of next generation terminal security management