题目:

求完全二叉树节点数。

思路:

满二叉树的节点数是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的更多相关文章

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

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

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

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

  3. leetcode面试准备:Count Complete Tree Nodes

    1 题目 Given a complete binary tree, count the number of nodes. In a complete binary tree every level, ...

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

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

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

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

  6. LeetCode Count Complete Tree Nodes

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

  7. [LeetCode] Count Complete Tree Nodes 求完全二叉树的节点个数

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

  8. leetcode_222 Count Complete Tree Nodes

    题目: Given a complete binary tree, count the number of nodes. Definition of a complete binary tree fr ...

  9. LeetCode OJ 222. Count Complete Tree Nodes

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

随机推荐

  1. 设置p标签可编辑

    一,只可编辑,粘贴复制字段长度不正常 <p contenteditable="true" >这是一个可编辑内容的p标签啦啦~</p> 二,可编辑,可粘贴复制 ...

  2. B-tree B+tree适合文件系统索引和MySQL索引

    B-树 B-树,这里的 B 表示 balance( 平衡的意思),B-树是一种多路自平衡的搜索树 它类似普通的平衡二叉树,不同的一点是B-树允许每个节点有更多的子节点.下图是 B-树的简化图. B-树 ...

  3. MSVC vs. MinGW 之dll玩转攻略手记【转

    一份粗糙的研究记录,有待补完和整理. MinGW:c -> o           gcc -c a.cc -> exe         gcc a.c libs.o -o a.exe ( ...

  4. 使用Linq对Hashtable和Dictionary<T,T>查询的效率比较

    近期做版本迭代任务,有一个在店铺头部展示店主所在的城市名称和省份名称的需求,店主信息表中保存了店主所在的城市Id和省份Id,由于原有业务复杂,要尽量减少Sql执行时间,所以不考虑join城市地区详细表 ...

  5. Android实现自带横线的EditText

    (一)问题 怎样实现带有横栏的EditText(像记事本的编辑界面那样)? (二)初步思路 1.通过修改EditText背景来实现(系统背景是一个框形图片,内部透明,替换为一个带有横栏的图片即可) 2 ...

  6. javascript中string与int之间的转换

    string转int javascript中提供了两种方法转换为数值(int): var str='15'; var str8='015'; var strChar='12abc'; //first ...

  7. Django 定时任务实现(django-crontab+command)

    一.编写自定义django-admin命令 注:利用django-admin自定义命令我们可以ORM框架对model进行操作,如:定时更新数据库,检测数据库状态..... Django为项目中每一个应 ...

  8. Heap-451. Sort Characters By Frequency

    Given a string, sort it in decreasing order based on the frequency of characters. Example 1: Input: ...

  9. ZZNU 2076(退役学长最后的神功 zz题)

    题目链接:http://acm.zznu.edu.cn/problem.php?pid=2076 输入一个T表示有T个样例每组实例一个整数n(0〈n〈1000接下来输入2*n个数字,代表一个2*n的矩 ...

  10. 利用DNSlog回显Weblogic(CVE-2017-10271) 漏洞执行命令结果

    作者:Armyzer0 Weblogic(CVE-2017-10271) 漏洞出来以后又是一波血雨腥风,正好我昨天测试的时候发现了一个存在这个漏洞的weblogic,但是他不回显咋办呢!让他返回执行结 ...