1 题目

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 last level h.

接口:public int countNodes(TreeNode root);

2 思路

思路1

用暴力法:不考虑完全二叉树的特性,直接遍历求节点数。---超时

复杂度: Time:O(N); Space:O(log N)

思路2

完全二叉树的节点数,可以用公式算出 2^h - 1. 如果高度不相等, 则递归调用 countNodes(root.left) + 1 + countNodes(root.right)

复杂度: Time:O(log N); Space:O(log N)

思路3

二分查找的思想,感觉和思路2差不多。但是代码写出来的效率高一些。

复杂度: Time:O(log N); Space:O(1)

3 代码

思路1

	public int countNodes0(TreeNode root) {
if (root == null)
return 0;
return countNodes(root.left) + 1 + countNodes(root.right);
}

思路2

	public int countNodes(TreeNode root) {
if (root == null)
return 0;
int leftHigh = 0, rightHigh = 0;
TreeNode lchild = root.left, rchild = root.right;
for (; lchild != null;) {
leftHigh++;
lchild = lchild.left;
}
for (; rchild != null;) {
rightHigh++;
rchild = rchild.right;
} if (leftHigh == rightHigh) {
return (2 << leftHigh) - 1;
} else {
return countNodes(root.left) + 1 + countNodes(root.right);
}
}

4 总结

计算阶乘的注意: (2 << leftHigh) - 1才不会超时,用Math.pow()超时。

二分查找思想,自己很不熟练。多找题目联系联系。

5 参考

leetcode面试准备:Count Complete Tree Nodes的更多相关文章

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

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

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

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

  3. LeetCode OJ 222. Count Complete Tree Nodes

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

  4. 【Leetcode】222. Count Complete Tree Nodes

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

  5. LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)

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

  6. 【leetcode】222. Count Complete Tree Nodes(完全二叉树)

    Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...

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

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

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

    2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...

  9. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

随机推荐

  1. XC一键锁屏应用

    XC一键锁屏,一键Android锁屏应用,彻底解放开关机键~ 下载地址: http://download.csdn.net/detail/jczmdeveloper/7329447

  2. 前后端分离--mock

    fekit mock 数据 > fekit server -m mock.config 配置mock.config 支持正则 module.exports = { /queryProductDe ...

  3. JavaScript 应用开发 #1:理解模型与集合

    在 < Backbone 应用实例 > 这个课程里面,我们会一起用 JavaScript 做一个小应用,它可以管理任务列表,应用可以创建新任务,编辑还有删除任务等等.这个实例非常好的演示了 ...

  4. PHP中的cookie创建取回删除;

    <?php $expire=time()+3600;//设置过期cookie时间 setcookie('yaoyuan',"webyaoyuan",$expire);//se ...

  5. 让IE支持Css3属性(圆角、阴影、渐变)

    >>>>>>>>>>>>>>>>>>>>>>>>> ...

  6. 在Abp框架中使用Mysql数据库的方法以及相关问题小记

    最近发现了一款DDD的框架 看起来不错,据说挺流弊的 刚好最近要弄点小东西,拿来试试也不错 苦于穷逼买不起高配服务器,只好装mysql数据库了 下面说下如何在该框架下使用Mysql数据库 打开项目后, ...

  7. Eclipse出现the type java.lang.CharSequence can't be resolved.

    出现这个问题我们需要安装一下JRE1.7这个版本,然后再项目里引入一下就可以了.

  8. SQL Execute语法.

    一,执行字符串: EXECUTE语句可以执行存放SQL语句的字符串变量,或直接执行SQL语句字符串. 语法:EXECUTE({@字符串变量|[N]’SQL语句字符串’}[+...n]) 例子:Decl ...

  9. angularjs跨域调取webservice

    1.配置 web.config <webServices> <!--必须添加--> <protocols> <add name="HttpGet&q ...

  10. [Machine Learning] Probabilistic Graphical Models:一、Introduction and Overview(2、Factors)

    一.什么是factors? 类似于function,将一个自变量空间投影到新空间.这个自变量空间叫做scope. 二.例子 如概率论中的联合分布,就是将不同变量值的组合映射到一个概率,概率和为1. 三 ...