leetcode222. Count Complete Tree Nodes

别再乱试了!数据恢复前必看的UFS Explorer Professional Recovery正确使用姿势 本文详细介绍了UFS Explorer Professional Recovery在数据恢复中的专业操作指南,帮助用户避免常见错误并提高恢复成功率。从紧急处理措施到磁盘镜像创建,再到高级RAID恢复技巧,全面解析了数据恢复的黄金法则。 阅读详情

记录这道题的目的主要是想复习一下计算时间复杂度的方法。题目描述是这样的:
Given a complete binary tree, count the number of nodes.

Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

计算一个完全二叉树(不是满二叉树)的所有结点数。
这里,我首先想到的是一个求解二叉树结点数的通法,也就是如下的代码:

int count(TreeNode* root)
{
    if(root==NULL) return 0; //Nothing to count
    return 1+count(root->left())+count(root->right());
}

这个算法可通用的所有二叉树,时间复杂度为:

O(n)

在这道题中明显会超时,原因是我们并没有用上完全二叉树的特性,上面题目描述的完全二叉树的特性如下:
1.除最后一层叶子结点以外,所有层的结点数都是满的。2.最后一层叶子结点的结点尽可能的靠近左边,即把左边占满。
那么根据这个特性,可以知道,对于一个结点来说,只要它满足左子树的深度等于右子树的深度。那么,就知道该树的结点数为:

20+21+22+2depth−1=2depth−1

根据这个特性,我们就可以节省不少计算,思路就是:一直递归到左深等于右深,可以得到该子树的结点数,然后相加,代码如下:

class Solution {
public:
    int countNodes(TreeNode* root) {
        if(root==NULL) return 0;
        TreeNode* l=root; TreeNode* r=root;
        int lh=0;int rh=0;
        while(l) {lh++;l=l->left;}
        while(r) {rh++;r=r->right;}
        if(lh==rh) return pow(2,lh)-1;
        return 1+countNodes(root->left)+countNodes(root->right);
    }
};

有意思的是这个算法的复杂度的计算。很明显的是,看最后的递归式:

1+countNodes(root->left)+countNodes(root->right)

可以看出是用分治的策略每次递归的时候对左子树和右子树进行操作,递归的深度明显就为logn.
而每次递归操作分别就是深度变化也就是

logn+(logn−1)+(logn−2)+....+1

最终结果的式子就为:
T(N)=T(N2)+c1∗logn=T(N4)+c1∗logn+c1∗(logn−1)=...=T(1)+c∗(logn+logn−1+logn−2+..+1)=logn∗logn

关于,O(logn*logn)大还是O(n),这是一个高中问题,这边我图方便直接用matlab画了个函数图比较:
这里写图片描述
深入解析JESD204B Periodic SYSREF在多ADC/DAC系统中的同步优化策略 本文深入探讨了JESD204B Periodic SYSREF在多ADC/DAC系统中的同步优化策略,针对雷达阵列和宽带通信系统中的同步精度问题,详细解析了硬件实现细节、参数配置实战经验及系统级优化方法。通过案例分析和技术要点,帮助工程师解决由温度漂移和时钟抖动引起的同步挑战,提升系统性能。 阅读详情

相关推荐

一个三极管和稳压管构成的简易稳压电源

一、对电压精度要求不高的场所可以使用本文中所述的简易稳压电源(电流变化引起 的基极-发射极的压降 变化 对输出电压来说可以容忍)二、根据负载实际需求选择三极管,不同的三极管集电极电流不同,电流放大倍数不同(1、尽量选择负载电流(iC 或 iE)上限时,hFE仍然比较理想的三极管,2、一般三极管对基极电流没有限制,可以适当调整基极电流以获得所需输出电流)

五花带点肉 1万+

leetcode 222: Count Complete Tree Nodes

leetcode 222: Count Complete Tree Nodes c++ java python

西施豆腐渣 1万+

CSR-DCF视频目标跟踪源码运行笔记

论文基本信息 论文标题:Discriminative Correlation Filter with Channel and Spatial Reliability 论文作者:Alan Lukezic等 论文出处:CVPR 2017 文章链接:https://arxiv.org/abs/1611.08461 补充材料:https://www.semanticscholar.org/paper/D...

越野者的博客 3397

闯关leetcode——222. Count Complete Tree Nodes

一棵深度为k的有n个结点的二叉树,对树中的结点按从上至下、从左到右的顺序进行编号,如果编号为i(1≤i≤n)的结点与满二叉树中编号为i的结点在二叉树中的位置相同,则这棵二叉树称为完全二叉树。假设完全二叉树是h深度,则其h-1深度层是满二叉树。h层元素全部靠左侧分布,即不存在“只有右节点,而无左节点”的情况。我们的解题思路就是:先找到满二叉树层的深度(不停遍历右子树),然后统计最后一层叶子节点数(先序遍历)。除最后一层无任何子节点外,每一层上的所有结点都有两个子结点的二叉树。完全二叉树是由满二叉树发展而来。

方亮的专栏 1077

安卓端目标检测模型部署血泪史:从NCNN/TFLite/MNN全军覆没到Torchscipts终见光明

公司需在Android设备部署目标检测模型,要求: ✅ 实时检测速度≥15FPS ✅ 模型精度损失<5% ✅ 兼容中低端安卓设备👉 GitHub项目地址:pytorch/android-demo-app/ObjectDetection我已上传这份源码到csdn下载准备模型(必须走这步才能跑通安卓端源码)如果您没有设置PyTorch环境来运行该脚本,可以下载模型文件此处转到“android演示app/ObjectDetection/app/src/main/assets”文件夹,然后跳过此步骤的其余部分,直

机器学习、深度学习、强化学习、迁移学习 1566

飞傲Fiio+foobar2000 实现dsd解码+灯正常显示

飞傲Fiio+foobar2000 实现dsd解码+灯正常显示。安装后可将DSD格式的数据通过ASIO驱动传输至DAC。安装后可识别DAC的ASIO驱动。安装后可识别DSD文件。

yanlei7993的博客 2301

LeetCode 222 Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the

Tech in Pieces 425

避坑指南:ZYNQ MPSOC QSPI固化时MT25QU256闪存的特殊配置

本文针对Xilinx ZYNQ UltraScale+ MPSoC平台,详细解析了使用Vitis工具将BOOT.bin固化至Micron MT25QU256ABA QSPI NOR Flash时的特殊配置与避坑指南。重点阐述了Init文件的真实作用、电压与频率的精确匹配策略,以及针对该闪存型号的初始化序列,帮助工程师解决烧录失败、启动异常等常见问题,确保系统可靠启动。

weixin_29175469的博客 270

LeetCode222——Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely fille

代码菌的blog 1923

电力现货市场出清机制解析:从理论到实践(附节点电价计算实例)

本文深入解析电力现货市场的核心出清机制,重点阐述了节点边际电价的计算原理与实践方法。通过对比系统边际电价、节点边际电价和分区边际电价三种模式,揭示了市场出清价格如何反映电力供需与网络阻塞。文章结合IEEE 30节点系统,提供了基于MATLAB和最优潮流模型的计算实例,详细展示了从构建模型到求解对偶变量以获取节点电价的全过程,为理解电力市场定价机制提供了从理论到实践的完整路径。

whisky的博客 755

基于STM32设计自动输液监测系统(241)

该系统集成了液位传感模块、光电传感器、电机模块、按键模块和OLED显示屏等多个功能模块,以实现全面的输液过程监控和控制。液位传感模块能够实时监测输液瓶中的液位,当液位到达限定值时,系统会自动控制步进电机夹紧输液管,并发出报警提醒,确保输液过程不会因液位过低而中断。

1341

leetcode 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as po

天使之翼 376

QT实现MQTT服务端(附完整源码)

QT实现MQTT服务端(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1252

LeetCode222 Count Complete Tree Nodes

Given acompletebinary tree, count the number of nodes. Note: Definition of a complete binary tree fromWikipedia: In a complete binary tree every level, except possibly the last, is completely fil...

李歇特冯-兹拜因巴哈的博客 172

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

Leetcode 222. Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled,

无名山丘,崛起成峰 1165

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

leetcode[222] Count Complete Tree Nodes

Given acompletebinary tree, count the number of nodes. Note: Definition of a complete binary tree fromWikipedia:In a complete binary tree every level, except possibly the last, is completely ...

weixin_30270889的博客 81

NTscan扫描工具

ntscan是一个扫描器,可以扫描没有密码和弱密码的共享,先到配置文件中填写猜测的用户名和密码,扫描过程中右侧出现的是扫描日志。

[LeetCode 222]Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely fille

coder 进阶的专栏 570

[leetcode 222]Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely fille

ER_Plough的专栏 584

Count Complete Tree Nodes - LeetCode 222

题目描述: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely

bu_min的博客 642

[leetcode] 222. Count Complete Tree Nodes

Description Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is comp...

Learning from the mistakes 221

【leetcode】222. Count Complete Tree Nodes

题目: 思路: 如果是满二叉树,2h−12^{h}-12h−1直接出答案。(这一步的优化可以稍微提高一下效率) 如果是不是满二叉树,笨办法,逐个计算吧。 代码实现: /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr),

zxc120389574的博客 155

LeetCode 222. Count Complete Tree Nodes

LeetCode 222. Count Complete Tree Nodes (Easy);主要知识点:tree;优先级:5

温迪今天刷题了吗 291

LeetCode-222 Count Complete Tree Nodes

题目描述 Given acompletebinary tree, count the number of nodes. Note: Definition of a complete binary tree fromWikipedia:In a complete binary tree every level, except possibly the last, is completel...

weixin_30381793的博客 151

Leetcode[222]-Count Complete Tree Nodes

Given a complete binary tree, count the number of nodes.Definition of a complete binary tree from Wikipedia: In a complete binary tree every level, except possibly the last, is completely filled, and

D.W 的专栏 2454

[leetcode] 222. Count Complete Tree Nodes 解题报告

题目链接:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from Wikipedia: In a complete binar

小榕流光的专栏 858
上一篇: Mac上安装boost库(在Xcode上)
下一篇: 揭秘UNIX文件缓存(the buffer cache)(实战篇)
写代码的柯长
博客等级 码龄11年 84粉丝 52原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值