【树】Count Complete Tree Nodes
题目:
求完全二叉树节点数。
思路:
满二叉树的节点数是2^k-1,k是树的深度。
所以我们可以先判断该树是否为满二叉树,然后是的话直接返回结果,如果不是递归地求解子树。
这样不用遍历所有的节点。复杂度小于O(N),比对所有点遍历复杂度要小,最好的情况是O(lgN)。
推算大概在O(lgN)~O(N)之间。
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var countNodes = function(root) {
if(root==null){
return 0;
} var l=root,r=root;
var lDep=0,rDep=0; while(l){
lDep++;
l=l.left;
} while(r){
rDep++;
r=r.right;
} if(lDep==rDep){
return Math.pow(2,lDep)-1;
}else{
return countNodes(root.left)+countNodes(root.right)+1;
}
};
【树】Count Complete Tree Nodes的更多相关文章
- leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes
完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...
- 完全二叉树的节点个数 Count Complete Tree Nodes
2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...
- leetcode面试准备:Count Complete Tree Nodes
1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...
- 【LeetCode】222. Count Complete Tree Nodes 解题报告(Python)
[LeetCode]222. Count Complete Tree Nodes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个 ...
- 【刷题-LeetCode】222. Count Complete Tree Nodes
Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...
- LeetCode Count Complete Tree Nodes
原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...
- [LeetCode] 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
题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...
- LeetCode OJ 222. Count Complete Tree Nodes
Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...
随机推荐
- python将字典中的数据保存到文件中
d = {'a':'aaa','b':'bbb'}s = str(d)f = open('dict.txt','w')f.writelines(s)f.close()
- JS数组去重算法实现
之前一段时间一直在准备面试, 因而博客太久没更新: 现在基本知识点都复习完毕, 接下来就分享下 面试的一些常见问题: 去正规的互联网公司笔试.面试有很大的概率会碰到 使用javascript实现数组去 ...
- Shell编程-05-Shell中条件测试与比较
目录 Shell脚本条件测试 Shell文件测试 Shell字符测试 Shell整数测试 Shell逻辑测试 Shell条件测试总结 Shell脚本条件测试 在Shell脚本中各种条件结构和流 ...
- hdu1302 The Snail
题目 题目大意: 一只蜗牛在H英尺高的底部,想爬到顶端.蜗牛可以在太阳升起的时候爬上U英尺,但是在晚上睡觉的时候会滑下D英尺.蜗牛的疲劳系数为F(百分比), ...
- onclick传参
var tema="<a title='打开' href='javascript:;' onclick='showKnowledgeMap(1,\" "+kl_na ...
- 利用ASIHTTPRequest访问网络
ASIHTTPRequest是第三方类库,ASIHTTPRequest对CFNetwork API进行了封装. 有如下特点: l 通过简单的接口,即可完成向服务端提交数据和从服务端获取数据的工作 l ...
- Angular6 学习笔记——组件详解之模板语法
angular6.x系列的学习笔记记录,仍在不断完善中,学习地址: https://www.angular.cn/guide/template-syntax http://www.ngfans.net ...
- WPF 的Listbox 滚动处理
操作需求场景:Listbox 高150 item 高150 listbox有几十个item ,希望鼠标滚轮滚动一次listbox 能滚动到下一个item, 代码实现: <Grid x:Name ...
- mvc基础知识(1)
复制大佬的,侵权请联系我主动删除 1.js/css合并 在之前的crud例子中,我们引入js/css脚本的方式和平常的web开发一样 <script src="~/Scripts/jq ...
- window10下Docker安装
首先window版本必须是10,如果是win7那么安装方法有所不同,win10是官方支持安装的.笔者安装的是Community社区版,版本信息如下: 1.去docker官网下载win10安装包: ht ...