【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)

标签(空格分隔): LeetCode

作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.me/


题目地址:https://leetcode.com/problems/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 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.

Example:

Input:
1
/ \
2 3
/ \ /
4 5 6 Output: 6

题目大意

求一个完全二叉树的节点个数。

解题方法

我知道这个肯定有简单算法的,否则可以直接遍历去求,那样没意义。

参考了222. Count Complete Tree Nodes的做法,在此感谢。

求完全二叉树的节点数目。注意完全二叉树和满二叉树Full Binary Tree的唯一区别是,完全二叉树最后一层的节点不满,而且假设最后一层有节点,都是从左边开始。 这样我们可以利用这个性质得到下面的结论:

  1. 假如左子树高度等于右子树高度,则右子树为完全二叉树,左子树为满二叉树。
  2. 假如高度不等,则左字数为完全二叉树,右子树为满二叉树。
  3. 求高度的时候只往左子树来找。

可以使用上面的结论得出本题的解法:

  1. 先构造一个getHeight方法, 用来求出二叉树的高度。这里我们只用求从根节点到最左端节点的长度。

  2. 求出根节点左子树高度leftHeight和根节点右子树高度rightHeight

    • 假如两者相等,那么说明左子树是满二叉树,而右子树可能是完全二叉树。
      – 我们可以返回 2 ^ leftHeight - 1 + 1 + countNodes(root.right)
      – 这里+1是因为把根节点也算进去,化简一下就是 1 << leftHeight + countNodes(root.right),返回结果
    • 否则两者不等,说明左子树是完全二叉树,右子树是满二叉树
      – 我们可以返回 2^ rightHeight - 1 + 1 + countNodeS(root.left)
      – 化简以后得到 1 << rightHeight + countNodes(root.left),返回结果
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None class Solution(object):
def countNodes(self, root):
"""
:type root: TreeNode
:rtype: int
"""
if not root: return 0
nodes = 0
left_height = self.getHeight(root.left)
right_height = self.getHeight(root.right)
if left_height == right_height:
nodes = 2 ** left_height + self.countNodes(root.right)
else:
nodes = 2 ** right_height + self.countNodes(root.left)
return nodes def getHeight(self, root):
height = 0
while root:
height += 1
root = root.left
return height

日期

2018 年 6 月 23 日 ———— 美好的周末要从刷题开始

【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)的更多相关文章

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

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

  2. Java for LeetCode 222 Count Complete Tree Nodes

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

  3. (medium)LeetCode 222.Count Complete Tree Nodes

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

  4. 【刷题笔记】LeetCode 222. Count Complete Tree Nodes

    题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...

  5. leetcode 222.Count Complete Tree Nodes

    完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...

  6. [leetcode]222. Count Complete Tree Nodes完全二叉树的节点数

    /* 满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么 是一个满二叉树.如果不是,那就把node算上,继续往下看,下边的可能是满二叉树 由于完全二叉树中有一些子满二叉 ...

  7. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  8. 【刷题-LeetCode】222. Count Complete Tree Nodes

    Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...

  9. LeetCode OJ 222. Count Complete Tree Nodes

    Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...

随机推荐

  1. SELECT的语法

    我们先回顾下正则表达式.下图: 描述像xy, xxy (B上转一圈), xyy, xxyy这样的字符串.然后可以进行字符串匹配.设计芯片都用Verilog语言而不是画门电路了.像x+y+这样的叫做re ...

  2. CentOS 6.4 下 Python 2.6 升级到 2.7

    一开始有这个需求,是因为用 YaH3C 替代 iNode 进行校园网认证时,CentOS 6.4下一直编译错误,提示找不到 Python 的某个模块,百度了一下,此模块是在 Python2.7 以上才 ...

  3. KVM配置

    安装依赖包(因最小化安装) [root@slave-master ~]# yum install -y vim wget tree lrzsz gcc gcc-c++ automake pcre pc ...

  4. RPC 框架

    RPC 谁能用通俗的语言解释一下什么是 RPC 框架? - 远程过程调用协议RPC(Remote Procedure Call Protocol) RPC就是要像调用本地的函数一样去调远程函数. 推荐 ...

  5. i++ 和 ++i 探究原理

    先看一个例子: package com.test; public class AutoIncrement { public static void main(String[] args) { int ...

  6. 使用beanFactory工厂实例化容器的方式实现单例模式

    //配置文件bean.properties(注意书写顺序) accountService=com.itheima.service.impl.AccountServiceImplaccountDao=c ...

  7. 【MySQL】亲测可用的教程筛选:安装与卸载

    windows版本的 安装看这篇,非常详细:https://www.cnblogs.com/winton-nfs/p/11524007.html 彻底清除:https://www.pianshen.c ...

  8. 【C/C++】函数的分文件编写

    创建同名的头文件(.h)和cpp文件. 在头文件里写函数声明,在cpp文件中写函数定义. 在cpp文件中写#include "xx.h" //自定义头文件名 框架(include ...

  9. 一个超简单的Microsoft Edge Extension

    这个比微软官网上的例子简单很多,适合入门.总共4个文件: https://files.cnblogs.com/files/blogs/714801/cet6wordpicker.zip 36KB 1. ...

  10. 用工具堆砌的DevOps 幻觉

    作者:顾宇 thoughtworks高级咨询师 在第一届 DevOpsDays结束后,DevOps 运动则如星火燎原之势在全球发展开来.随着 DevOps 思想的不断传播,相对的质疑和批评也从未停止过 ...