当前位置:网站首页>leetcode刷题:二叉树10(完全二叉树的节点个数)
leetcode刷题:二叉树10(完全二叉树的节点个数)
2022-07-05 19:52:00 【涛涛英语学不进去】
222.完全二叉树的节点个数
给出一个完全二叉树,求出该树的节点个数。
示例 1:
- 输入:root = [1,2,3,4,5,6]
- 输出:6
示例 2:
- 输入:root = []
- 输出:0
示例 3:
- 输入:root = [1]
- 输出:1
提示:
- 树中节点的数目范围是[0, 5 * 10^4]
- 0 <= Node.val <= 5 * 10^4
- 题目数据保证输入的树是 完全二叉树
直接层序遍历查,不失为一种快乐的方法。
package com.programmercarl.tree;
import java.util.ArrayDeque;
import java.util.Deque;
/** * @ClassName CountNodes * @Descriotion TODO * @Author nitaotao * @Date 2022/7/4 10:15 * @Version 1.0 * https://leetcode.cn/problems/count-complete-tree-nodes/ * 222. 完全二叉树的节点个数 **/
public class CountNodes {
public int countNodes(TreeNode root) {
/** * 层序遍历 */
if (root == null) {
return 0;
}
Deque<TreeNode> deque = new ArrayDeque<TreeNode>();
deque.offer(root);
int sum = 0;
while (!deque.isEmpty()) {
int size = deque.size();
while (size > 0) {
root = deque.pop();
sum++;
if (root.left != null) {
deque.offer(root.left);
}
if (root.right != null) {
deque.offer(root.right);
}
size--;
}
}
return sum;
}
}
按照满二叉树的性质,节点数 = 前n-1层节点数 + 最后一层节点数
前n-1层节点数 = 2^(n-1) -1
/** * 根据完全二叉树的性质计算 * * @param root * @return */
public int countNodes(TreeNode root) {
if (root == null) {
return 0;
}
int leftDepth = getDepth(root.left);
int rightDepth = getDepth(root.right);
if (leftDepth == rightDepth) {
//如果深度相等,则左边为满二叉树
// leftDepth是从当前子树的头结点开始计算深度,不是两棵树的根结点
// ( 2 ^ leftDepth - 1 ) + 1 1为两棵树的根结点
return ((1 << leftDepth) - 1) + 1 + countNodes(root.right);
} else {
//右子树为满二叉树
return ((1 << rightDepth) - 1) + 1 + countNodes(root.left);
}
}
/** * 从当前子树 开始计算深度 * * @param root * @return */
public int getDepth(TreeNode root) {
int depth = 0;
while (root != null) {
root = root.left;
depth++;
}
return depth;
}
边栏推荐
- 大厂面试必备技能,2022Android不死我不倒
- Four methods of random number generation | random | math | threadlocalrandom | securityrandom
- 深度學習 卷積神經網絡(CNN)基礎
- 如何安全快速地从 Centos迁移到openEuler
- C#应用程序界面开发基础——窗体控制(5)——分组类控件
- Force buckle 1200 Minimum absolute difference
- Xaas trap: all things serve (possible) is not what it really needs
- Force buckle 729 My schedule I
- ACM getting started Day1
- Worthy of being a boss, byte Daniel spent eight months on another masterpiece
猜你喜欢
【无标题】
图嵌入Graph embedding学习笔记
使用 RepositoryProvider简化父子组件的传值
Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
集合
Add data to excel small and medium-sized cases through poi
webuploader文件上传 拖拽上传 进度监听 类型控制 上传结果监听控件
leetcode刷题:二叉树16(路径总和)
Let's talk about threadlocalinsecurerandom
JVMRandom不可设置种子|问题追溯|源码追溯
随机推荐
Based on vs2017 and cmake GUI configuration, zxing and opencv are used in win10 x64 environment, and simple detection of data matrix code is realized
Summer Challenge database Xueba notes, quick review of exams / interviews~
Parler de threadlocal insecurerandom
leetcode刷题:二叉树17(从中序与后序遍历序列构造二叉树)
Reinforcement learning - learning notes 4 | actor critical
【无标题】
Fundamentals of shell programming (Part 8: branch statements -case in)
Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
如何安全快速地从 Centos迁移到openEuler
使用 RepositoryProvider简化父子组件的传值
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Apprentissage du projet MMO I: préchauffage
Is the education of caiqiantang reliable and safe?
Webuploader file upload drag upload progress monitoring type control upload result monitoring control
Float. The specific meaning of the return value of floattorawintbits is to convert float into byte array
XaaS 陷阱:万物皆服务(可能)并不是IT真正需要的东西
Four methods of random number generation | random | math | threadlocalrandom | securityrandom
不愧是大佬,字节大牛耗时八个月又一力作
建立自己的网站(16)
《乔布斯传》英文原著重点词汇笔记(十二)【 chapter ten & eleven】