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.

题目大意:给定一个完全二叉树,返回这个二叉树的节点数量。

解题思路:一开始想着层次遍历,O(n),结果TLE,后来优化了下,采用递归的方式来做,总节点数等于1+左子树的数量+右子树的数量,因为是完全二叉树,所以对于子树是满二叉树的可以直接计算出结果并返回。

public class Solution {
public int countNodes(TreeNode root) {
if(root==null){
return 0;
}
TreeNode left =root,right=root;
int cnt = 0;
while(right!=null){
left=left.left;
right=right.right;
cnt++;
}
if(left==null){
return (1<<cnt)-1;
}
return 1+countNodes(root.left)+countNodes(root.right);
}
}

Count Complete Tree Nodes ——LeetCode的更多相关文章

  1. Count Complete Tree Nodes || LeetCode

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * s ...

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

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

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

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

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

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

  5. 【刷题-LeetCode】222. Count Complete Tree Nodes

    Count Complete Tree Nodes Given a complete binary tree, count the number of nodes. Note: Definition ...

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

    2018-09-25 16:36:25 问题描述: 问题求解: 单纯遍历了一遍,emmm,果然TLE. 解题思路就是比较左边树高度和右边树高度,如果相等,那么就是一个满二叉树,返回1 << ...

  7. LeetCode Count Complete Tree Nodes

    原题链接在这里:https://leetcode.com/problems/count-complete-tree-nodes/ Given a complete binary tree, count ...

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

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

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

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

随机推荐

  1. 布局重用 include merge ViewStub

    在布局优化中,Androi的官方提到了这三种布局<include />.<merge />.<ViewStub />,并介绍了这三种布局各有的优势,下面也是简单说一 ...

  2. 转--Oracle DB 服务器系统时间修改问题与 SCN 关系的深入研究

    论坛里一个朋友说将DB 服务器系统时间往往后修改了3个月(从11年改成10年),启动DB报600的错误. 一. 先做个测试 1.1 关闭DB SQL> shutdown immediate Da ...

  3. 委托、 Lambda表达式和事件——事件

    /* * 由SharpDevelop创建. * 用户: David Huang * 日期: 2015/7/31 * 时间: 14:21 */ using System; namespace 事件 { ...

  4. Spring MVC返回json数据给Android端

    原先做Android项目时,服务端接口一直是别人写的,自己拿来调用一下,但下个项目,接口也要自己搞定了,我想用Spring MVC框架来提供接口,这两天便抽空浅学了一下该框架以及该框架如何返回json ...

  5. 344. Reverse String(C++)

    344. Reverse String Write a function that takes a string as input and returns the string reversed. E ...

  6. iOS打包ipa 让别人设备安装你的App

    首先推荐一本书<一步一步学习iOS 5编程(第二版) – PDF 中文版>在一本学习IOS入门很不错的书籍,目前,这是第一本介绍iOS 5.x 和 Xcode 4.3 的中文版书籍,尤其适 ...

  7. 【模板】【网络流】Dinic

    /* 唐代杜荀鹤 <小松> 自小刺头深草里,而今渐觉出蓬蒿. 时人不识凌云木,直待凌云始道高. */ #include <iostream> #include <cstd ...

  8. TextView控件

    1.手动创建(不建议): TextView tv = new TextView(this); tv.setContent("你好"); setContentView(tv); 2. ...

  9. php微信支付接口开发程序

    php微信支付接口开发程序讲解 微信支付接口现在也慢慢的像支付宝一个可以利用api接口来实现第三方网站或应用进行支付了, 下文整理了一个php微信支付接口开发程序并且己测试,有兴趣的朋友可进入参考. ...

  10. Java多线程:常用的实现多线程的两种方式

    之所以说是常用的,是因为通过还可以通过java.util.concurrent包中的线程池来实现多线程.关于线程池的内容,我们以后会详细介绍;现在,先对的Thread和Runnable进行了解.本章内 ...