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

题目:

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

Definition of a complete binary tree from Wikipedia:
In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive at the last level h.

题解:

Complete Tree的定义是左右深度差不大于1.

递归调用函数,终止条件两个,一个是root == null, return 0. 另一个是左右高度相同说明是满树, return 2^height-1.

若是左右高度不同,递归调用求 左子树包含Node数 + 右子树包含Node数 + 1(自身).

Note:1. 用Math.pow(), 注意arguments 和 return type 都是double, 此处会TLE.

2. 用<<来完成幂运算,但注意<<的precedence比+,-还要低,需要加括号.

Time Complexity: O(h^2). worst case对于每一层都要求一遍当前点到leaf得深度.

假设只有最底层多了一个TreeNode 那么计算height就需要每层计算一遍h + (h-1) + (h-2) + ... + 1 = O(h^2).

Space: O(h), stack space.

AC  Java:

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int countNodes(TreeNode root) {
if(root == null){
return 0;
}
TreeNode p = root;
TreeNode q = root;
int leftDepth = 0;
int rightDepth = 0;
while(p!=null){
leftDepth++;
p = p.left;
}
while(q!=null){
rightDepth++;
q = q.right;
} if(leftDepth == rightDepth){
return (1<<leftDepth) - 1;
}else{
return countNodes(root.left) + 1 + countNodes(root.right);
}
}
}

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

  1. [LeetCode] 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

    Description: Given a complete binary tree, count the number of nodes. In a complete binary tree ever ...

  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 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

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

  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. 完全二叉树的节点个数 Count Complete Tree Nodes

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

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

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

  9. 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 ...

随机推荐

  1. ztree学习之异步加载节点(一)

    ztreedemo.jsp: <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  2. state配置语言实战

    修改配置文件:(base用来放初始化环境.prod用来放生产配置环境) [root@super65 ~]# vim /etc/salt/master [root@super65 ~]# mkdir - ...

  3. Qt 程序退出时断言错误——_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),由setAttribute(Qt::WA_DeleteOnClose)引起

    最近在学习QT,自己仿写了一个简单的QT绘图程序,但是在退出时总是报错,断言错误: 报错主要问题在_BLOCK_TYPE_IS_VALID(pHead->nBlockUse),是在关闭窗口时报的 ...

  4. ThinkPHP几个配置文件的位置

    1.常用的ThinkPHP\Conf\convention.php 2.ThinkPHP/Lib/Behavior/ParseTemplateBehavior.class.php模板引擎相关配置

  5. IOS 开发的官方文档链接

    下面这些文章都是苹果官方的开发文档,非常有用: iOS Developer Library https://developer.apple.com/library/ios/navigation/ 总入 ...

  6. POJ 1419 Graph Coloring(最大独立集/补图的最大团)

    Graph Coloring Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4893   Accepted: 2271   ...

  7. PHP生成随机密码的4种方法及性能对比

    PHP生成随机密码的4种方法及性能对比 http://www.php100.com/html/it/biancheng/2015/0422/8926.html 来源:露兜博客   时间:2015-04 ...

  8. IMP-00009: 导出文件异常结束

    今天准备从生产库向测试库进行数据导入,结果在imp导入的时候遇到" IMP-00009: 导出文件异常结束" 错误,google一下,发现可能有如下原因导致 imp的数据太大,没有 ...

  9. Front-end Developer Interview Questions

    Front-end-Developer-Interview-Questions A list of helpful front-end related questions you can use to ...

  10. php常用[字符串]函数

    nl2br 功能:化换行符为<br> <?php $str = "cat isn't \n dog"; $result = nl2br($str); echo $ ...