题目链接: https://leetcode-cn.com/problems/count-complete-tree-nodes

难度:中等

通过率:57.4%

题目描述:

给出一个 完全二叉树 ,求出该树的节点个数。

说明:

完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置。若最底层为第h 层,则该层包含 1~ 2h 个节点。

示例:

输入:
1
/ \
2 3
/ \ /
4 5 6 输出: 6

思路:

暴力,每个节点都访问一下,看有多少个

时间复杂度:\(O(n)\)

class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root: return 0
return 1 + self.countNodes(root.left) + self.countNodes(root.right)

这样不是完全二叉树也可以计算,但是没有用到完全二叉树的性质!

我们可以通过运用性质,完全二叉树的节点个数等于 \(2^{h} - 1\),

所以有,时间复杂度:\(O(log(n)^2)\)

class Solution:
def countNodes(self, root: TreeNode) -> int:
if not root: return 0
left_height = 0
left_node = root
right_height = 0
right_node = root
while left_node:
left_node = left_node.left
left_height += 1
while right_node:
right_node = right_node.right
right_height += 1
if left_height == right_height:
return pow(2,left_height) - 1
return 1 + self.countNodes(root.left) + self.countNodes(root.right)

[LeetCode] 完全二叉树的节点个数的更多相关文章

  1. Leetcode 222.完全二叉树的节点个数

    完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最 ...

  2. Java实现 LeetCode 222 完全二叉树的节点个数

    222. 完全二叉树的节点个数 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集 ...

  3. leetcode-222完全二叉树的节点个数

    题目 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置. ...

  4. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Definition of a complete binary tree from W ...

  5. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  6. LeetCode 222.完全二叉树的节点个数(C++)

    给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底 ...

  7. Leetcode 222:完全二叉树的节点个数

    题目 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置. ...

  8. LeetCode 222. 完全二叉树的节点个数(Count Complete Tree Nodes)

    题目描述 给出一个完全二叉树,求出该树的节点个数. 说明: 完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位 ...

  9. [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

随机推荐

  1. [CF666E]Forensic Examination:后缀自动机+线段树合并

    分析 用到了两个小套路: 使用线段树合并维护广义后缀自动机的\(right\)集合. 查询\(S[L,R]\)在\(T\)中的出现次数:给\(T\)建SAM,在上面跑\(S\),跑到\(R\)的时候先 ...

  2. Watir单元库

    http://www.cnblogs.com/Javame/p/4045229.html test: #require 'net/http' #require 'uri' #url = URI.par ...

  3. Oracle Rac 测试

      #还是使用之前的脚步来进行测试 #Author : Kconnie Pong Oracle@PONGDB:~> more load_balance.sh #!/bin/bash ..} do ...

  4. const变量的修改实践

    https://bbs.csdn.net/topics/110049293 #include <iostream> using namespace std; int main(){ cou ...

  5. Ubuntu16.04小白入门分享之 玩转Ruby你需要安装什么软件(持续更新)

    Ubuntu提示功能很强大,一般如果你想安装什么软件,可以直接输入名字,然后会有提示,安装格式一般为: sudo apt install 名字 Ubuntu14.04/16.04命令行快速安装Ruby ...

  6. [mysql]root用户登录mysql,输入密码后报错:Access denied for user 'root'@'localhost'

    问题如下: wangju-G4:~$ mysql -u root -p Enter password: ERROR (): Access denied for user 'root'@'localho ...

  7. flutter 图片圆角

    return Center( child: Container( padding: EdgeInsets.only(left: 10), width: 120, height: 80, child: ...

  8. ControlTemplate in WPF —— TextBox

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  9. jquery 教程网

  10. 基于 Node.js 的服务器自动化部署搭建实录

    基于 Node.js 的服务器自动化部署搭建实录 在服务器上安装 Node.js 编写拉取仓库.重启服务器脚本 配置 Github 仓库的 Webhook 设置 配置 Node.js 脚本 其他问题 ...