对于一般的二叉树,统计节点数目遍历一遍就可以了,但是这样时间复杂度O(n),一下就被卡住了。

这题首先要明白的是,我们只需要知道叶子节点的数目就能统计出总节点树。

想法1:

  既然是完全二叉树,我肯定是从左子树开始看,如果左子树不完整,右子树就不用再遍历了。由此形成一个递归的搜索过程,先搜索左子树,如果不完整,直接停止搜索,统计完毕;否则,还要再搜索右子树。

  这样就能避开完全搜索遍历整棵树,但是当树接近满树的时候实际上还是将整颗树遍历了一遍。

想法2:

  完全二叉树不同于满树的一点是,我们只能肯定它的最左边那个叶子节点肯定是存在的(这里说存在不太准确,实际就把完全二叉树当满树看,然后那些不满的地方就认为是叶子节点不存在)。所以一颗完全二叉树的高度只需要一路向左就能求得。现在告诉你一颗完全二叉树可能不是满树,从根节点的角度,就要看,到底从哪开始不满的。

按照定义,不满的节点肯定在右边。如果我们求解根节点的右子树的高度=H - 1,说明左子树肯定是满的,不用再看了;否则,右节点高度 = H - 2,说明root->right的叶节点都缺了,右边不用再看了。

  有没有发现这种想法使得我们几乎一次排除了一半的元素,所以这题有一个标签叫做binary search,因为感觉想法上很像。

    int countNodes(TreeNode* root) {
int height = 0;
auto p = root;
while(p != NULL){
height++;
p = p->left;
}
if(height < 2)
return height;
int countLeaf = 0;
int curHeight = height;
while(root){
auto pr = root->right;
int heightR = 0;
while(pr != NULL){
heightR++;
pr = pr->left;
}
if (heightR == 0){
countLeaf+= 1;
break;
}
else if(heightR == curHeight - 1){
//we don't need to look at the left child any more
//because it must be full
countLeaf += (1 << (heightR - 1));
root = root->right;
}
else{
//we don't need to look at the right child any more
//because there no leaf node
root = root->left;
}
curHeight -= 1;
}
return countLeaf + (1 << (height - 1)) - 1;
}

LeetCode222 Count Complete Tree Nodes的更多相关文章

  1. LeetCode222——Count Complete Tree Nodes

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

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

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

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

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

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

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

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

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

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

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

  7. LeetCode Count Complete Tree Nodes

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

  8. [Swift]LeetCode222. 完全二叉树的节点个数 | Count Complete Tree Nodes

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

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

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

随机推荐

  1. hdu 1421

    时隔多日,又回来啃dp... 题意:有n件物品,搬k次,每搬一个消耗的疲劳值为两件物品重量之差的平方,求最小的疲劳消耗 状态转移方程:dp[i][j] = min((dp[i-2][j-1]+(s[i ...

  2. jmeter 建立一个扩展LDAP测试计划

    添加用户 第一步你想做的每一个JMeter测试计划是添加一个线程组元素. 线程组告诉JMeter的用户数量你想模拟,用户应该发送的次数 请求,他们应该发送的请求的数量. 继续添加 线程组 首先选择元素 ...

  3. [maven] 常用仓库地址

    共有的仓库 http://mvnrepository.com/ http://repo1.maven.org/maven2/ http://repository.jboss.com/maven2/ h ...

  4. hdu 3367(Pseudoforest ) (最大生成树)

    Pseudoforest Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  5. jquery mobile 请求数据方法执行时显示加载中提示框

    在jquery mobile开发中,经常需要调用ajax方法,异步获取数据,如果异步获取数据方法由于网速等等的原因,会有一个反应时间,如果能在点击按钮后数据处理期间,给一个正在加载的提示,客户体验会更 ...

  6. 如何判断一个Div是否可视区域,判断div是否可见

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. enum使用总结

    enum的一般使用方法是它会占用最大的成员长度 然后我忘记的是enum还可以这样使用 enum ExctState { START, SUCCEED, FAILURE=6, REJECT, }; 这样 ...

  8. group_concat函数与find_in_set()函数相结合

    一:group_concat函数详解 1.语法如下: group_concat([DISTINCT] 要连接的字段 [Order BY ASC/DESC 排序字段] [Separator '分隔符'] ...

  9. node 事件循环

    什么是事件循环 Node只运行在一个单一线程上,至少从Node.js开发者的角度是这样的.在底层, Node是通过libuv来实现多线程的. Libuv库负责Node API的执行.它将不同的任务分配 ...

  10. GO工程和包

    转载:http://blog.csdn.net/achelloworld/article/details/43956831 在Go语言中,包是函数与数据的集合,用package定义一个包,包的名字不一 ...