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.

解题思路:

计算完全二叉树节点,直接递归会TLE,一个不错的思路是判断是否为满二叉树,然后进行递归,JAVA实现如下:

    public int countNodes(TreeNode root) {
if(root==null)
return 0;
int leftHeight=1,rightHeight=1;
TreeNode temp=root.left;
while(temp!=null){
temp=temp.left;
leftHeight++;
}
temp=root.right;
while(temp!=null){
temp=temp.right;
rightHeight++;
}
if(leftHeight==rightHeight)
return (1<<leftHeight)-1;
return countNodes(root.left)+countNodes(root.right)+1;
}

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

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

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

  2. (medium)LeetCode 222.Count Complete Tree Nodes

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

  3. 【刷题笔记】LeetCode 222. Count Complete Tree Nodes

    题意 给一棵 complete binary tree,数数看一共有多少个结点.做题链接 直观做法:递归 var countNodes = function(root) { if(root===nul ...

  4. leetcode 222.Count Complete Tree Nodes

    完全二叉树是从左边开始一点点填充节点的,因此需要计算所有的节点的个数. 则分别从左边和右边来进行传递的,当左右是完全二叉树的时候,其节点个数就是pow(2,h)-1. /** * Definition ...

  5. [leetcode]222. Count Complete Tree Nodes完全二叉树的节点数

    /* 满二叉树的特点是2^n-1,对于完全二叉树,一个node如果左右子树深度相同,那么 是一个满二叉树.如果不是,那就把node算上,继续往下看,下边的可能是满二叉树 由于完全二叉树中有一些子满二叉 ...

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

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

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

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

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

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

  9. LeetCode OJ 222. Count Complete Tree Nodes

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

随机推荐

  1. realloc,malloc,calloc函数的区别

    from:http://www.cnblogs.com/BlueTzar/articles/1136549.html realloc,malloc,calloc的区别 三个函数的申明分别是: void ...

  2. linux 生成KEY的方法与使用

    转自:http://blog.163.com/tqq_0716/blog/static/7690741220110611350344/ 服务器A: 192.168.1.1 服务器B: 192.168. ...

  3. Linux服务器管理: 日志管理(二)

    日志的轮替 1.日志文件的命名规则 a.如果配置文件中拥有"dateext"参数,那么日志会用日期来作为日志文件的后缀,例如:"secure-20150630" ...

  4. OB函数

    ob_start                    打开输出控制缓冲 ob_get_contents        返回输出缓冲区内容 ob_clean                   清空( ...

  5. 2015年12月13日 spring初级知识讲解(四)面向切面的Spring

    2015年12月13日 具体内容待补充...

  6. hdu.1254.推箱子(bfs + 优先队列)

    推箱子 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  7. Logistic 回归(sigmoid函数,手机的评价,梯度上升,批处理梯度,随机梯度,从疝气病症预测病马的死亡率

    (手机的颜色,大小,用户体验来加权统计总体的值)极大似然估计MLE 1.Logistic回归 Logistic regression (逻辑回归),是一种分类方法,用于二分类问题(即输出只有两种).如 ...

  8. 【C语言入门教程】4.6 指针 和 数组

    数组在内存中以顺序的形式存放,数组的第一个存储单元的地址即数组的首地址.对一维数组来说,直接引用数组名就能获得该数组的首地址.指针变量可以存放于其内容相同的数组首地址,也可以指向某一具体的数组元素.通 ...

  9. Android在TextView中实现RichText风格

    参考: Android实战技巧:用TextView实现Rich Text---在同一个TextView中设置不同的字体风格 Demo: private SpannableStringBuilder c ...

  10. Oracle添加数据报文字与格式字符串不匹配错误

    今天在学习Oracle时碰到一个错:文字与格式字符串不匹配. 我在Oracle数据库中创建了一张表: --创建员工表employee create table employee ( empon ) n ...