【树】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 ...
随机推荐
- ansible facts 获取硬件信息
facts 指的是 ansible_facts 变量,ansible 中使用 setup 模块来获取,包含系统的大部分基础硬件信息, [root@10_1_162_39 host_vars]# ll ...
- Python调用Google翻译
出自:http://blog.csdn.net/zhaoyl03/article/details/8830806 最近想动手做一个文档自动下载器,需要模拟浏览器的行为.虽然感觉思路上没有困难,但在技术 ...
- 大文件上传插件webupload插件
版权所有 2009-2018荆门泽优软件有限公司 保留所有权利 官方网站:http://www.ncmem.com/ 产品首页:http://www.ncmem.com/webapp/up6.2/in ...
- Robotframework-Appium 之常用API(二)
续接上一文,更多API详细如下: 注:更多官方详情信息见 http://robotframework.org/robotframework/ 28. Name: Install App Source: ...
- jmap的几个操作要慎用
JDK中带有了一堆的工具是可以用来查看运行状况,排查问题的,但对于这些工具还是要比较清楚执行后会发生什么,否则有可能会因为执行了一个命令就导致严重故障,重点讲下影响比较大的jmap. 最主要的危险操作 ...
- [转自知乎] 从github上下载单个文件夹
原文地址: 如何从 GitHub 上下载单个文件夹? 注意:如果是在公司网络环境的话需要配置可以访问外网的代理,否则 svn checkout 时会出错.
- visual studio 2015 rc &cordova -hello world
初始环境,用来看看书,电影,上上网的win8,所以一切从头开始. 1,首先还是装visual studio 2015 rc吧,目前只放出在线安装,所以要很长很长时间.不过有新闻说很快要实现中国网友至 ...
- mongodb 问题
启动mongodb时,提示Unclean shutdown detected mongodb,解决方法很简单 mongod --repair --dbpath D:\MongoDB\blog 不用 ...
- [翻译Joel On Software]选择一门语言/Choosing a language
Joel on Software Choosing a language 选择一门语言 by Joel Spolsky Sunday, May 05,2002 Why do developerscho ...
- Java内存模型以及Volatile、Synchronize关键字的疑问
1.众所周知,java的内存模型是一个主内存,每个线程都有一个工作内存空间,那么主内存同步到工作内存是什么时候发生的呢?工作内存同步会主内存又是什么时候发生的呢? 在cpu进行线程切换时就会发生这些同 ...