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.
数二叉树的节点数首先肯定是可以用暴力解法去解问题的,但是这样总是会timeout:

 class Solution {
public:
int countNodes(TreeNode* root) {
if(!root) return ;
total++;
countNodes(root->left);
countNodes(root->right);
return total;
}
private:
int total;
};

其他的办法就是对完全二叉树而言,其一直向左走和一直向右边、走只可能是相同的或者相差1,如果相同那么起节点个数就是2^h - 1否曾再递归的加上左右节点的和就可以了。

 /**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
if(!root) return ;
TreeNode * leftNode = root;
TreeNode * rightNode = root;
int left = ;
int right = ;
while(leftNode->left){
left++;
leftNode=leftNode->left;
}
while(rightNode->right){
right++;
rightNode = rightNode->right;
}
if(left == right) return ( << (left + )) - ;
else return + countNodes(root->left) + countNodes(root->right);
}
};

写的比较乱哈 ,凑合着看看把。

下面是java代码,首先肯定是递归形式的,不出所料,同样会TLE:

 /**
* 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;
else
return 1 + countNodes(root.left) + countNodes(root.right);
}
}

考虑到完全二叉树的性质,下面这个写法就不会TLE了:

 public class Solution {
public int countNodes(TreeNode root) {
if(root == null)
return 0;
int right = 1, left = 1;
TreeNode leftNode = root;
TreeNode rightNode = root;
while(leftNode.left != null){
leftNode = leftNode.left;
left++;
}
while(rightNode.right != null){
rightNode = rightNode.right;
right++;
}
if(left == right)
return (1 << left) - 1;
else
return 1 + countNodes(root.left) + countNodes(root.right); //注意这里不要忘记计算本身的这个节点
}
}

LeetCode OJ:Count Complete Tree Nodes(完全二叉树的节点数目)的更多相关文章

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

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

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

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

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

  4. leetcode之Count Complete Tree Nodes

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

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

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

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

  7. 222 Count Complete Tree Nodes 完全二叉树的节点个数

    给出一个完全二叉树,求出该树的节点个数.完全二叉树的定义如下:在完全二叉树中,除了最底层节点可能没填满外,其余每层节点数都达到最大值,并且最下面一层的节点都集中在该层最左边的若干位置.若最底层为第 h ...

  8. leetcode 222.Count Complete Tree Nodes

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

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

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

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

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

随机推荐

  1. More on Class Loading and Initialization

    上一篇博客中对于类的加载和初始化进行了详细的说明,但上一篇博客代码中的main()所在的类为导出类, 对其中一些问题的理解可能会引起误导和不明确,所以补充这篇博客进一步说明.以下面的代码为例进行说明: ...

  2. 【转】通过fio工具,测试SATA,SAS,SSD 读写性能

      转自:http://blog.csdn.net/killmice/article/details/42745937

  3. iOS 设置 延迟执行 与 取消延迟执行 方法 以及对 run loop 初步认识

    之前开发过程中经常会有需求会使用 NSObject中的"performSelector:withObject:afterDelay:"做方法延迟执行的处理, 但是 还没有什么地方需 ...

  4. Ajax在jQuery中的应用---ajax()方法

    在jQuery中,$.ajax()方法是最底层的方法,也是功能最强的方法.其调用的语法格式为: $.ajax([options]) 其中,可选项参数[options]为$.ajax()方法中的请求设置 ...

  5. CentOS 5下freeswitch中集成使用ekho实现TTS功能三

    四:在freeswitch中调用ekho 注:在测试过程中该语音包好像没用 FreeSWITCH 中文语音包测试版fssounds.zip 在/usr/local/freeswitch/sounds/ ...

  6. STM32F4XX高效驱动篇2 I2C

    说到I2C很多用过STMF10X硬件I2C方式的工程师,都感觉有点头痛.大部分还是使用软件模拟的方式,I2C由于一般的工作频率是400,100KHz.所以在平凡读取,或所读数据量大时,使用这模拟的方式 ...

  7. CSS3透明背景表单

    在线演示 本地下载

  8. git上面创建个人简历-链接

    github创建个人在线简历: https://segmentfault.com/a/1190000006820290

  9. iOS应用适配IPV6

    网络收集,连接如下: 针对苹果iOS最新审核要求为应用兼容IPv6 iOS应用支持IPV6,就那点事儿 iOS 适配iPV6的修改(AF及其他第三方库)

  10. INSPIRED启示录 读书笔记 - 第3章 产品管理与项目管理

    互联网让两者变得不同 在传统的零售软件领域,产品经理常常兼任项目经理的工作,随着互联网的发展,两者的职责区别也越来越明显 产品管理的职责是探索(定义)有价值的.可用的.可行的产品 项目管理的职责是关注 ...