/*
满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么
是一个满二叉树。如果不是,那就把node算上,继续往下看,下边的可能是满二叉树
由于完全二叉树中有一些子满二叉树,所以可以省时间
*/
public int countNodes(TreeNode root) {
if (root==null) return 0;
int l = getLeft(root);
int r = getRight(root);
return (l == r)?(1<<l)-1:countNodes(root.left)+countNodes(root.right)+1;
}
//获取左子树深度
private int getLeft(TreeNode root)
{
if (root==null) return 0;
return getLeft(root.left)+1;
}
//获取右子树深度
private int getRight(TreeNode root)
{
if (root==null) return 0;
return getRight(root.right)+1;
}

这个题直接遍历会超时。利用了满二叉树的特点,完全二叉树中满二叉树还是有不少的。

对于满二叉树的定义,国内的定义除了每个节点都左右子树外,要求所有叶子节点都在一层上,但是国际上的只要前一个条件就可以。这里说的满二叉树是国内定义的那种。

完全二叉树相对于满二叉树,最后一层可能缺失最右边几个节点。

以后遇见完全二叉树,可以多考虑下满二叉树的特点。

[leetcode]222. Count Complete Tree Nodes完全二叉树的节点数的更多相关文章

  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. (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 ...

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

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

  4. 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 ...

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

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

  6. leetcode 222.Count Complete Tree Nodes

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

  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 解题报告(Python)

    [LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...

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

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

随机推荐

  1. 更改ubuntu的分辨率

    乘号使用xyz的x打出来

  2. 深度优先遍历&广度优先遍历

    二叉树的前序遍历,中序遍历,后序遍历 树的遍历: 先根遍历--访问根结点,按照从左至右顺序先根遍历根结点的每一颗子树. 后根遍历--按照从左至右顺序后根遍历根结点的每一颗子树,访问根结点. 先根:AB ...

  3. Python中的enumerate函数的作用

    enumerate函数是将一个可迭代对象中元素,按元素顺序每个增加一个索引值,将其组成一个索引序列,利用它可以同时获得索引和值,这样做的目的是为了将一个可迭代对象中元素组成一个"索引,值&q ...

  4. instanceof constructor Object.prototype.tostring.call ( [] )区别 数组和 对象的3中方法

  5. Hangfire&Autofac与ASP.NET CORE注入失败

    Hangfire.Autofac与ASP.NET CORE注入失败 项目里面使用了Hangfire,因为之前没用过吧,遇到了个问题,就是使用了ico容器后,再用Hangfire总是注入不上对象,总是后 ...

  6. go学习第四天

    昨天通宵加班,暂停了一天学习,今天再偷懒下,学习半个小时

  7. window下kettle安装

    参考这篇文章 http://note.youdao.com/noteshare?id=a8c536ba952a48d60d7ea8f2cc61a94b

  8. Android Studio中SVN的使用

    1.忽略文件 1)这种方式,每次新建一个项目都要添加,并不是全局的. .idea文件夹 .gradle文件夹 所有的build文件夹 所有的.iml文件 local.properties文件 2)使用 ...

  9. 六、Jmeter测试元件-测试计划

    启动Jmeter时,会默认建好一个测试计划,启动一个Jmeter只有个测试计划 测试用到的源码下载地址 https://www.cnblogs.com/fanfancs/p/13557062.html ...

  10. json 注释

    一.背景 今天聊个小东西,Json的的的注释.Json十分常见,大家用的很多,在语法上,规范的Json要求,文件里不可以写注释.原因呢,我调查了一下: I removed comments from ...