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.

计算一个完全二叉树的所有节点

我看到“二叉树”和“所有节点”两个关键词后,立即想到用递归的方法解决,于是就有了下面的代码。

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right; }; int countNodes(struct TreeNode* root) {
int count = ;
if (!root){
return count;
}
if (root->left){
count += countNodes(root->left);
}
if (root->right){
count += countNodes(root->right);
}
return count;
}

提交之后,出现错误:

Last executed input:Special Judge: very large tree

看样子效率是个问题啊。从完全二叉树的特性上入手再试试。

leetcode之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. 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 ...

  3. (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 ...

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

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

  5. leetcode 222.Count Complete Tree Nodes

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

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

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

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

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

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

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

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

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

随机推荐

  1. Modbus Poll :Byte Missing Error或CRC Error

    原因: 1.通信线路受干扰或是路线接触不良:    用显示器测量物理电平信号   2.从机工作不正常: 检测电源不正常或查程序bug   3.PC主机串口不正常: PC串口2.3脚答短接用串口调试器测 ...

  2. 第一次见4.3K电阻

    今天焊RC522的实验板,接收电阻买的是5.1K,焊接时发现丝层写的是432,阻为4.26K.理论值应该是4.3K

  3. hibernate学习五(关系映射多对一与一对多)

    一.多对一 多对一(或者一对多):在学生与老师的情况下,一个老师可以教多个学生,但一个学生只能被教一个老师教: 对于类:在多的那方拥有一的那方的一个实体 二.修改student.java和teache ...

  4. HDU2177:取(2堆)石子游戏(威佐夫博弈)

    Problem Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同 ...

  5. iOS 分类和继承

    iOS 中分类(Categories) 和 继承(Inherit)有相同的功能,但在一些细节上又有差异,简单介绍一下两者的异同. 分类可以在不知道系统类源代码的情况下,为这个类添加新的方法.分类只能用 ...

  6. Android硬件加速

    Android从3.0(API Level 11)开始,在绘制View的时候支持硬件加速,充分利用GPU的特性,使得绘制更加平滑,但是会多消耗一些内存. 开启或关闭硬件加速: 由于硬件加速自身并非完美 ...

  7. 使用Eclipse建立Maven的SpringMVC项目

    非常感谢博客的大神,让我成功地构建项目 http://limingnihao.iteye.com/blog/830409 但在依照这篇文章构建构建时遇到了一些问题,在这里总结一下: 问题一.2.3.2 ...

  8. jQuery的自定义事件——滚轮

    这个案例类似于在地图上滚动滚轮,能缩小或者放大地图,分别用zoomIn和zoomOut来命名. 代码如下: //JS部分:<script src="jquery-1.10.2.min. ...

  9. 文本框的placeholder属性

    在文本框中添加placeholder属性,用于文本框处于未输入状态时,模糊显示输入提示,当用户进行输入时,不需要删除该提示文字. 如: <input type="text" ...

  10. oracle数据库创建用户,并且给用户授权

    参考文档: http://www.blogjava.net/wolfman09/archive/2009/05/01/268536.html 一:创建用户 create user username i ...