题意

给一棵 complete binary tree,数数看一共有多少个结点。做题链接

直观做法:递归

var countNodes = function(root) {
if(root===null) return 0;
return 1+countNodes(root.left)+countNodes(root.right);
};

老实说,一道难度为 medium 的题目,这么几秒钟的时间就做出来,我心中有一种不真实感。

所以,我看了一下 discuss 区其他人的解法,看是不是我自己想的不够深入。

结果发现,我这个做法没错,就是时间复杂度不够优化,如果这道题要真正成为一道难度为medium的题目,需要限制时间复杂度。

下面是一种据说是 O(log(n)^2) 的解法,很聪明,根据不同树结构选择计算左树结点数或右树节点数,可以省去访问大部分结点的时间。

优化做法:计算树高,观察树结构

var countNodes = function(root) {
let h = height(root);
return h === 0 ? 0 :
height(root.right)=== h-1 ? (1 << (h-1)) + countNodes(root.right) :
                                        (1 << (h-2)) + countNodes(root.left) ;
};
function height(root){
return root === null? 0 : 1+height(root.left);
}

首先,height 函数,没啥好说的,跟普通的计算树高度函数不同点在于,充分利用了 complete binary tree 的特点,更加省事。

其次,要理解根据 root 的高度 和 右子树高度的不同情况的选择。

情况一:

height(root) === 0

这个情况没啥好说的,就是边界情况,啥结点都没有,返回0;

情况二:

height(root.right) === h-1

这个情况就是下面图这种情况,右子树至少最左边有一个结点(这个结点把右子树的高度撑到 h-1):

这种情况下,左子树是完整的,它的节点数是  2^(h-1)-1 ,加上根结点,等于  2^(h-1) ,也就是  1 << (h-1) (图中绿色结点总数)。

情况三:

height(root.right) === h-2

这种情况就是右子树最后一层没有任何结点,左子树至少最后一层最左边有一个结点:

这种情况下,右子树是完整的,它的节点数是  2^(h-2)-1 ,加上根结点,等于  2^(h-2) ,也就是  1<<(h-2) (图中绿色结点总数)。

再次优化:用遍历取代递归

直接遍历所有的节点,可以省下反复计算 height 的不必要的时间。

因为很明显,子树的高度 跟 父树的高度 息息相关。如果通过递归,下一次还得重新计算高度值;而用遍历,直接  h--;  就得到子树高度值。(当然你也可以在递归函数中把高度当成参数传递进去,但是不够简洁,没必要)

var countNodes = function(root) {
let h = height(root), nodes = 0;
while(root!==null){
if(height(root.right)===h-1){
nodes += 1 << (h-1);
root = root.right;
}
else{
nodes += 1 << (h-2);
root = root.left;
}
h--;
}
return nodes;
};
function height(root){
return root===null? 0 : 1+height(root.left);
}

第一种简单方法的优化

最开始我用了一种最简单的方法:

var countNodes = function(root) {
if(root===null) return 0;
return 1+countNodes(root.left)+countNodes(root.right);
};

LeetCode 的 discuss 区看到 一种优化方法:

var countNodes = function(root) {
if(root===null) return 0;
let left = root.left, right = root.right, height = 1;
while(right !== null){
left = left.left;
right = right.right;
height++;
}
if(left === null) return (1 << height)-1;
return 1 + countNodes(root.left) + countNodes(root.right);
};

这种方法其实也是利用了 Complete Binary Tree 的特点,如果一棵树是完整的(就是它所有树叶都处于同一层),那么在 右子树 为  null  之前, left = left.left; right = right.right; 最左最右两个分枝同时向下延伸,最后一定同时抵达树叶。

这种方法的优化之处在于,当遇到的子树的所有树叶都位于同一层时,它会直接返回该子树的结点数。像下面这样的树,当遇到结点 3, 5, 8 时,不会再调用函数递归,而是直接计算出整颗子树的结点数  (1 << height) - 1 。

总结

要了解数据结构的特点,并且充分利用这些特点来省事。

PS:上面全部代码为 JavaScript

【刷题笔记】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. 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

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

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

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

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

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

  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. linux下创建公钥

    # linux下创建公钥 链接地址:https://www.cnblogs.com/ibyte/p/6086630.html 示例: scp -r /home/yutang/.ssh/id_rsa.p ...

  2. winfrom窗体属性

  3. 第7章 性能和可靠性模式 Failover Cluster(故障转移群集)

    上下文 您已经决定在设计或修改基础结构层时使用群集以提供高度可用的服务. 问题 您应该如何设计一个高度可用的基础结构层,来防止因单台服务器或它所运行的软件出现故障而导致的服务丢失? 影响因素 在设计高 ...

  4. SQL Server将数据导出到SQL脚本文件

    http://www.studyofnet.com/news/list-8883.2-1-4.html 一.SQL Server 2008将数据导出到SQL脚本文件 1.打开SQL Server200 ...

  5. ML:流形学习

    很多原理性的东西需要有基础性的理解,还是篇幅过少,所以讲解的不是特别的清晰. 原文链接:http://blog.sciencenet.cn/blog-722391-583413.html 流形(man ...

  6. Python快速定位工作目录

    原文链接:http://www.cnblogs.com/wdong/archive/2010/08/19/1802951.html 常年奋斗在编码一线的同学,应该都深有体会,工作久了,很多项目文件.技 ...

  7. SVD分解.潜语义分析.PythonCode

    原文链接:http://www.cnblogs.com/appler/archive/2012/02/02/2335886.html 原始英文链接:http://www.puffinwarellc.c ...

  8. 基于MapReduce的贝叶斯网络算法研究参考文献

    原文链接(系列):http://blog.csdn.net/XuanZuoNuo/article/details/10472219 论文: 加速贝叶斯网络:Accelerating Bayesian ...

  9. PhotoZoom Classic 7有什么用?高品质的放大模糊图片!

    PhotoZoom Classic 7专门用于放大照片,同时保持质量.该软件配备了BenVista独特的S-Spline技术,可轻松超越Photoshop的双三次插值等替代解决方案. PhotoZoo ...

  10. JS 封装一个判断闰年平年的方法 aa(nian)

    nn(2017) function nn (nian){ if(nian%4 == 0 && nian%100 !== 0 || nian%400 ==0 ) { alert(&quo ...