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. 在eclipse中的tomcat内存设置

    设置步骤如下: 1.点击eclipse上的debug图标旁边的下拉箭头 2.然后选择Run Configurations, 3.系统弹出设置tomcat配置页面,在Argument中末尾添加参数中的V ...

  2. 文件上传工具类 UploadUtil.java

    package com.util; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ja ...

  3. [spring security] spring security 4 基础Demo

    依赖包: <properties> <junit.version>4.11</junit.version> <spring.version>4.1.6. ...

  4. 【linux】输出重定向

    “>”:把正确结果输出到一个文件 [root@andon ~]# ls > 1 [root@andon ~]# cat 1 1 anaconda-ks.cfg install.log in ...

  5. MySQL加载并执行SQL脚本文件

    第一种方法: 命令行下(未连接数据库) ,输入 mysql -h localhost -u root -p123456 < C:\db.sql 第二种方法: 命令行下(已连接数据库,此时的提示符 ...

  6. sublime_text3 用户配置

    { "auto_complete_triggers": [ { "characters": "", "selector" ...

  7. winform实现word转换为PDF(.doc)

    注意:实现word转换为PDF文件,本人安装Office为2013; word以后缀为.doc为例实现文件类型转换,具体方式如下所示: 实现步骤: 1.添加命名空间引用——using Microsof ...

  8. 有关JSP注释

    最近学习过滤器的时候,dispatcher可以指定过滤器被Servlet容器拦截的方式,可以是REQUEST.INCLUDE.FORWARD.ERROR,默认是REQUEST方式. 现在有两个filt ...

  9. 理解Socket编程【转载】

    “一切皆Socket!” 话虽些许夸张,但是事实也是,现在的网络编程几乎都是用的socket. ——有感于实际编程和开源项目研究. 我们深谙信息交流的价值,那网络中进程之间如何通信,如我们每天打开浏览 ...

  10. C#继承的用法

    using System; namespace 继承 { public class cat { private string _name = null; private int _age = 0; p ...