当前位置:网站首页>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;
}

边栏推荐
- PHP uses ueditor to upload pictures and add watermarks
- CADD课程学习(7)-- 模拟靶点和小分子相互作用 (半柔性对接 AutoDock)
- 95后阿里P7晒出工资单:狠补了这个,真香...
- C application interface development foundation - form control (5) - grouping control
- Zhongang Mining: analysis of the current market supply situation of the global fluorite industry in 2022
- Is it safe to open a mobile stock account? Is it reliable?
- gst-launch的-v参数
- 太牛了,看这篇足矣了
- Bitcoinwin (BCW) was invited to attend Hanoi traders fair 2022
- What do software test engineers do? How about the prospect of treatment?
猜你喜欢

Base du réseau neuronal de convolution d'apprentissage profond (CNN)

力扣 1200. 最小绝对差
![[FAQ] summary of common causes and solutions of Huawei account service error 907135701](/img/1d/0e716533237c0e4463f5d6357395bd.png)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701

软件测试是干什么的?学习有啥要求?

Jvmrandom cannot set seeds | problem tracing | source code tracing

力扣 729. 我的日程安排表 I

leetcode刷题:二叉树13(相同的树)

third-party dynamic library (libcudnn.so) that Paddle depends on is not configured correctl

如何安全快速地从 Centos迁移到openEuler
Successful entry into Baidu, 35K monthly salary, 2022 Android development interview answer
随机推荐
What are general items
再忙不能忘安全
leetcode刷题:二叉树18(最大二叉树)
Inventory of the most complete low code / no code platforms in the whole network: Jiandao cloud, partner cloud, Mingdao cloud, Qingliu, xurong cloud, Jijian cloud, treelab, nailing · Yida, Tencent clo
安卓面试宝典,2022Android面试笔试总结
C#应用程序界面开发基础——窗体控制(6)——菜单栏、工具栏和状态栏控件
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
UWB ultra wideband positioning technology, real-time centimeter level high-precision positioning application, ultra wideband transmission technology
Tasks in GStreamer
The city chain technology Digital Innovation Strategy Summit was successfully held
函数的概念及语法
Is it safe to open a mobile stock account? Is it reliable?
Worthy of being a boss, byte Daniel spent eight months on another masterpiece
众昂矿业:2022年全球萤石行业市场供给现状分析
Gstreamer中的task
Parler de threadlocal insecurerandom
SecureRandom那些事|真伪随机数
How to safely and quickly migrate from CentOS to openeuler
四万字长文说operator new & operator delete
Fundamentals of shell programming (Part 8: branch statements -case in)