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.

解法一:递归超时

代码如下:

/**
* 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;
int num1=0,num2=0;
num1=count(root.left);
num2=count(root.right);
return 1+num1+num2; }
int count(TreeNode root){
if(root==null) return 0;
int num1=0,num2=0;
if(root.left==null && root.right==null)
return 1;
if(root.left!=null)
num1=count(root.left);
if(root.right!=null)
num2=count(root.right);
return 1+num1+num2;
}
}

  运行结果:

解法二:层次遍历,队列,超时

代码如下:

/**
* 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; Queue<TreeNode> q=new LinkedList<>();
q.offer(root);
int num=1;
TreeNode tmp=null;
while(!q.isEmpty()){
tmp=q.poll();
if(tmp.left!=null){
q.offer(tmp.left);
num++;
}
if(tmp.right!=null){
q.offer(tmp.right);
num++;
}
}
return num;
} }

  解法三:如果从某节点一直向左的高度 = 一直向右的高度, 那么以该节点为root的子树一定是complete binary tree. 而 complete binary tree的节点数,可以用公式算出 2^h - 1. 如果高度不相等, 则递归调用 return countNode(left) + countNode(right) + 1.  复杂度为O(h^2)

代码如下:

/**
* 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; int l = getLeft(root) + 1;
int r = getRight(root) + 1; if(l==r) {
return (2<<(l-1)) - 1;
} else {
return countNodes(root.left) + countNodes(root.right) + 1;
}
} private int getLeft(TreeNode root) {
int count = 0;
while(root.left!=null) {
root = root.left;
++count;
}
return count;
} private int getRight(TreeNode root) {
int count = 0;
while(root.right!=null) {
root = root.right;
++count;
}
return count;
}
}

  运行结果:

(medium)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. 【刷题笔记】LeetCode 222. Count Complete Tree Nodes

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

  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 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. 根据N种规格中的M种规格值生成的全部规格组合的一种算法

    近来在开发SKU模块的时候,遇到这样一个需求,某种商品有N(用未知数N来表示是因为规格的数组由用户制定且随时可以编辑的,所以对程序来说,它是一个未知数)类规格,每一类规格又有M个规格值,各种规格值的组 ...

  2. centos curl版本nss改成openssl

    在centos 6.2的系统里面的curl支持的https是nss版本的,而不是openssl的,所以在php使用curl访问https的时候会报Unable to load client key - ...

  3. [转]何为C10K问题

    我在学习网络编程的时候经常看到C10K问题,那么究竟什么是C10K问题呢?我看到了一篇好文章就转了过来,原文地址为:c10k问题 所谓c10k问题,指的是服务器同时支持成千上万个客户端的问题,也就是c ...

  4. jQuery Wookmark 瀑布流布局

    瀑布流布局非常适合大量图片的展示,一改过去裁剪图片尺寸统一的排版,每张图片都能完全展示,并错落有致,让人眼前一亮. 版本: jQuery v1.4.3+ jQuery Wookmark Load v1 ...

  5. php 添加redis扩展(一)

    phpredis 下载地址 http://pan.baidu.com/s/1dFFK0gh  提取码:见评论,(~..)亲身试验,可行 1.下载到目录 cd /usr/local/src 2.解压 t ...

  6. SPOJ #429 Simple Numbers Conversion

    This is simply a human work simulation - exactly reproducing how you do it by hand. Nothing special. ...

  7. apidoc,一个相当不错的文档生成器

    http://apidocjs.com/ 例子:myapp目录下的MyCode.java /** * * @api {get} /company/list 获取公司信息 * @apiName 获取公司 ...

  8. Dell vsotro 14 3000系列从win10重装win7

    1. F2启动进入新的BIOS界面,首先Disable Secure Boot,然后把UEFI改为Legeacy模式,当然是改不回来的,不知道为什么: 2. 插入U盘(老毛桃+UEFI启动镜像): 3 ...

  9. select,epool,pool解释

    内容主要来自搜狗实验室技术交流文档, 编写链接数巨大的高负载服务器程序时,经典的多线程模式和select模式都不再适合了.应该采用epool/kqueue/dev_pool来捕获IO事件. ----- ...

  10. (C/C++) Interview in English - Basic concepts.

    Question Key words Anwser A assignment operator abstract class It is a class that has one or more pu ...