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. 转载Code First Migrations更新数据库架构的具体步骤

    [转载] Code First Migrations更新数据库结构的具体步骤 我对 CodeFirst 的理解,与之对应的有 ModelFirst与  DatabaseFirst ,三者各有千秋,依项 ...

  2. iOS---RunLoop深度剖析

    RunLoop 前言 RunLoop是iOS/OS开发中比较基础的一个概念,在苹果开发中用在事件处理,延迟加载,屏幕刷新等功能的处理,其实抛开语言,RunLoop是一个的架构模式,也就是RunLoop ...

  3. 如何让Iconfont作用到content伪类中

    沿用同行思密达的方法整理了一下;前提是先引入iconfont字体图标(引用线上iconfont查看上篇随笔):比如 其中&#是开头用以标明这是字符实体,x表示这是十六进制,而CSS的cont ...

  4. 网页标签图片如何保存&下载?

    最简单的方法就是鼠标右键,查看网页源代码,ctrl+f输入favicon.ico,一般网站都是这个

  5. LVS DR模型

    1,环境 VMWare10, CentOS6.3 2,LVS DR网络规划 所有机器都只需要一张网卡,给Director的eth0网卡起个别名eth0:1即VIP的值:给RealServer的lo网卡 ...

  6. Linux HugePages及MySQL 大页配置

    http://blog.csdn.net/dba_waterbin/article/details/9669929       ㈠ HugePages简介             HugePages是 ...

  7. PAT 1008

    1008. Elevator (20) The highest building in our city has only one elevator. A request list is made u ...

  8. 23、从头学Android之ContentProvider .

    http://blog.csdn.net/jiahui524/article/details/7016430 应用场景: 在Android官方指出的Android的数据存储方式总共有五种,分别是:Sh ...

  9. Apache的RewriteRule规则详细介绍

    R[=code](force redirect) 强制外部重定向 (rkyW z强制在替代字符串加上http://thishost[:thisport]/前缀重定向到外部的URL.如果code不指定, ...

  10. Internationalization

    Internationalization If you are building a site for an international audience, you will likely want ...