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 2hnodes inclusive at the last level h.

题目大意:给定一个完全二叉树,返回这个二叉树的节点数量。

解题思路:一开始想着层次遍历,O(n),结果TLE,后来优化了下,采用递归的方式来做,总节点数等于1+左子树的数量+右子树的数量,因为是完全二叉树,所以对于子树是满二叉树的可以直接计算出结果并返回。

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

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

  1. Count Complete Tree Nodes || LeetCode

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...

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

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

  7. LeetCode Count Complete Tree Nodes

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

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

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

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

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

随机推荐

  1. asp.net mvc4 使用java异步提交form表单时出现[object object] has no method ajaxSubmit

    最近接手了一个单子,说大不大,只是功能不少,开发过程中遇到该问题 先看脚本截图: 本以为是笔误,哪儿写错了,可是看来看去,都没发现有不合适的地方,对比过网上很多代码,都差不多,于是各种方式的,各种原因 ...

  2. bootstrap 下的 validation插件

    http://reactiveraven.github.io/jqBootstrapValidation/

  3. IDEA SVN1.8 问题解决

    转自 http://blog.jetbrains.com/idea/2013/12/subversion-1-8-and-intellij-idea-13/

  4. Cacti优化之spine轮询器

    由于效率的原因,在需要大量采集数据时,如果使用自带的cmd.php轮询器会比较慢,1分钟1次的采集频率可能无法完成轮询所有的被监控的机器,从而可能导致部分监控项目不出图或图形断断续续.为了解决效率问题 ...

  5. 设置 textField.placeholder的颜色和大小

    textField.placeholder = @"请输入手机号码"; [textField setValue:[UIColor blue] forKeyPath:@"_ ...

  6. [转]PHP取整函数:ceil,floor,round,intval的区别详细解析

    我们经常用到的PHP取整函数,主要是:ceil,floor,round,intval. 1.ceil -- 进一法取整 说明float ceil ( float value ) 返回不小于 value ...

  7. hdoj 2040

    #include<stdio.h>int i,j,s1,s2;int cha(int a,int b){ s1=0; s2=0;   for(i=1;i<a;i++)   {    ...

  8. 初涉JavaScript模式 (3) : 字面量

    什么是字面量? 在编程语言中,字面量是一种表示值的记法.例如,"Hello, World!" 在许多语言中都表示一个字符串字面量(string literal ),JavaScri ...

  9. 将listBox中信息显示在dataGridview中,操作datagridview后删除listBox信息和SQL数据库信息 续(浅谈listBox..)

    应用场景      对datagridview控件使用了解,以及操作datagridview选中的信息删除,并且有二次确认后才删除用户信息.相应的删除listbox中用户信息,下面一起看看需要哪些准备 ...

  10. 常用排序算法集合-C实现

    之前熟悉C的时候写着玩的,就当做笔记用吧: #include<stdio.h> #include<stdlib.h> #include<string.h> #inc ...