题目:

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.

解题:

假设用常规的解法一个个遍历,就是O(n)时间复杂度 。会不通过,这边就不写O(n)的代码了。由于是全然二叉树,满二叉树有一个性质是节点数等于2^h-1,h为高度,所以能够这样推断节点的左右高度是不是一样,假设是一样说明是满二叉树,就能够用刚才的公式。假设左右不相等就递归计算左右节点。

代码:

public static int countNodes(TreeNode root) {
if(root==null)
return 0;
else {
int left=getLeftHeight(root);
int right=getRightHeight(root);
if(left==right)
return (1<<left)-1;
else {
return countNodes(root.right)+countNodes(root.left)+1;
}
}
} public static int getRightHeight(TreeNode root) {
int height=0;
while(root!=null)
{
height++;
root=root.left;
}
return height; } public static int getLeftHeight(TreeNode root) {
int height=0;
while(root!=null)
{
height++;
root=root.right;
}
return height; }

LeetCode222 Count CompleteTree Nodes(计算全然二叉树的节点数) Java 题解的更多相关文章

  1. C++计算二叉树的节点数和高度

    用struct结构体的写法: /* * description: 计算二叉树的层数和节点数 * writeby: nick * date: 2012-10-23 16:16 * */ #include ...

  2. C递归算法与栈的分析,非全然二叉树遍历分析---ShinePans

                 对于递归,这里面的分析最好当然是用图形的方式来分析了.这里来总结一下 1.首先对于栈的理解: 先进后出,后进先出 先进后出 2.在进行非全然二叉树的存储之后,我们要做的是对其 ...

  3. 二叉树遍历等基本操作(Java实现)

    前中后序遍历递归实现+层序遍历: 树的结点类代码: public class TreeNode<Value extends Comparable<? super Value>> ...

  4. 李洪强iOS经典面试题35-按层遍历二叉树的节点

    李洪强iOS经典面试题35-按层遍历二叉树的节点 问题 给你一棵二叉树,请按层输出其的节点值,即:按从上到下,从左到右的顺序. 例如,如果给你如下一棵二叉树:    3   / \  9  20   ...

  5. 求二叉树第K层的节点个数+求二叉树叶子节点的个数

    size_t _FindLeafSize(Node* root)     //求二叉树叶子节点的个数    {        //static size_t count = 0;        if ...

  6. 面试常见二叉树算法题集锦-Java实现

    1.求二叉树的深度或者说最大深度 /* ***1.求二叉树的深度或者说最大深度 */ public static int maxDepth(TreeNode root){ if(root==null) ...

  7. 六:二叉树中第k层节点个数与二叉树叶子节点个数

    二叉树中第k层节点个数 递归解法: (1)假设二叉树为空或者k<1返回0 (2)假设二叉树不为空而且k==1.返回1 (3)假设二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树 ...

  8. R语言:计算现金工资的币数

    新入职的员工,有的没有相应银行卡,需要计算现金工资的币数.实发工资,一般取整数. 简化计算,纸币面值只有100.10.1.4278除以100等于42余78,78除以10等于7余8,8除以1等于8. 复 ...

  9. LeetCode222——Count Complete Tree Nodes

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

随机推荐

  1. SQL中树形分层数据的查询优化

    在数据查询中,从2008开始SQL Server提供了一个新的数据类型hierarchyid,专门用来操作层次型数据结构. hierarchyid  类型对层次结构树中有关单个节点的信息进行逻辑编码的 ...

  2. Cannot convert type SomeClass to 'T'

    以下代码会出问题: public static T Protect<T>(Func<T> func, UserLevel pageRole) where T : ActionR ...

  3. Python集合模块collections

    collections collections是Python内建的一个集合模块,提供了许多有用的集合类. namedtuple 我们知道tuple可以表示不变集合,例如,一个点的二维坐标就可以表示成: ...

  4. Backbone.js 的 View 中定义事件

    使用 Backbone 的 View 时,可以象传统 jQuery 那样定义事件,$("selector").click(function(){...}).幸运的是 Backbon ...

  5. C++类中的访问权限问题

    纠结的东西: private,public,protected方法的访问范围.(public继承下)private: 只能由该类中的函数.其友元函数访问,不能被任何其他访问,该类的对象也不能访问. p ...

  6. linux bash关闭标准输出1(exec 1<&-)后重新打开

    linux bash shell的再次学习. 文件描述符: stdin,stdout 和 stderr 的文件描述符分别是 0,1 和 2(一个文件描述符说白了就是文件系统为了跟踪这个打开的文件而分配 ...

  7. redis信息相关集群

    转: http://www.runoob.com/redis/redis-install.html //redis的安装与运维相关 http://zhou123.blog.51cto.com/4355 ...

  8. mybaits动态SQL中的DECIMAL

    数据库:mysql数据库字段类型:decimal(11,2)java程序类型:java.math.BigDecimal 使用mybatis的动态语句 <if test ="money! ...

  9. Ant编译utf-8非法字符:/65279 解决方法

    原文链接:http://blog.csdn.net/xiyuan1999/article/details/5989336   Ant编译utf-8非法字符:/65279 解决方法   使用ant编译j ...

  10. Android Handler 消息处理使用

    本文内容 环境 演示 Handler 消息处理 参考资料 Handler 有两个主要作用或者说是步骤:发送消息和处理消息.在新启动的线程中发送消息,在主线程中获取.并处理消息.Android 平台只允 ...