【刷题笔记】LeetCode 222. Count Complete Tree Nodes
题意
给一棵 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的更多相关文章
- [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数
Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...
- 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 ...
- (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 ...
- leetcode 222.Count Complete Tree Nodes
完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...
- [leetcode]222. Count Complete Tree Nodes完全二叉树的节点数
/* 满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么 是一个满二叉树.如果不是,那就把node算上,继续往下看,下边的可能是满二叉树 由于完全二叉树中有一些子满二叉 ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- LeetCode OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
随机推荐
- jTemplates的教程,包括{#if}{#foreach}{#for}的简单使用
最近在做一些局部刷新的分页工作,一般不使用既成的插件的话,就是在脚本里面重新渲染一个局部的html,把需要局部分页的模块重写一遍,还需要在控制器里再定义一个方法返回所需的局部数据,这种做法相当冗余,所 ...
- Java基础5一数组的常见应用算法
常用算法 1.冒泡排序: 原理:比较两个相邻的元素,将值大的元素交换至右端 示例: public static void bubbleSort(int[] a) { int n = a.length; ...
- 用户 'NT Service\MSSQLServerOLAPService' 登录失败
初学SSAS,部署微软官方示例项目AdventureWorksDW2012Multidimensional时出现错误:用户 'NT Service\MSSQLServerOLAPService' 登录 ...
- Win10 BackgroundTask
1.这里面详细的说明了后台任务的搭建 调用等 提示: 1.BackgroundTaskRegistration 里面有这两个事件 OnCompleted/Progress 主要用来UpdateUI 这 ...
- java学习笔记4——返回值
这个简单,返回值就是计算结果. 打个比方:个表格中我只要结果,不要经过,这个返回值就是结果.这个过程就是函数. 另外还有一个函数套用一个函数,被套用的函数的结果作为一个返回值给套用的外层函使用.比如: ...
- UVa340(Master-Mind Hints)未完成
#include<stdio.h> int main() { int num,a[100],i,j,b[100]; while(scanf("%d",&num) ...
- win 7环境下java环境变量的配置
http://www.cnblogs.com/zhj5chengfeng/archive/2013/01/01/2841253.html %Java_Home%\bin;%Java_Home%\jre ...
- (转)基于MVC4+EasyUI的Web开发框架经验总结(5)--使用HTML编辑控件CKEditor和CKFinder
http://www.cnblogs.com/wuhuacong/p/3780356.html Web开发上有很多HTML的编辑控件,如CKEditor.kindeditor等等,很多都做的很好,本文 ...
- Redmine 甘特图导出 PDF 和 PNG 中文乱码问题
Redmine使用了RMagick来处理图片,fpdf处理PDF,并在调用时设定了字体PDF中文字体 redmine 中关于PDF字体设置的代码 case pdf_encoding ...
- 记一次vip视频破解过程(爱奇艺 芒果 腾讯 优酷 )
1. 在爱奇艺或者优酷中拿到视频的url地址.此时拿到的是加密地址(也可以直接在牛巴巴里面搜名字然后开f12跟踪路由) 2.进入牛巴巴vip视频解析网站.粘贴拿到的url.点击解析 3.f12在net ...