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) {
List<TreeNode> list=PreOrder(root);
return list.size(); }
public List<TreeNode> PreOrder(TreeNode root){
List<TreeNode> list=new ArrayList<>();
if(root==null)
return list; list.add(root);
if(root.left!=null)
list.addAll(PreOrder(root.left));
if(root.right!=null)
list.addAll(PreOrder(root.right));
return list;
}
}

222. Count Complete Tree Nodes的更多相关文章

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

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

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

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

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

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

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

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

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

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

  7. LeetCode OJ 222. Count Complete Tree Nodes

    Total Accepted: 32628 Total Submissions: 129569 Difficulty: Medium Given a complete binary tree, cou ...

  8. 【Leetcode】222. Count Complete Tree Nodes

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

  9. 222. Count Complete Tree Nodes -- 求完全二叉树节点个数

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

随机推荐

  1. 关于JavaScript是否会阻塞图片加载

    <?php //1.js.php sleep(5); file_put_contents("tmp.txt", __FILE__.'->'.__LINE__.' -&g ...

  2. POJ 3299 Humidex 难度:0

    题目链接:http://poj.org/problem?id=3299 #include <iostream> #include <iomanip> using namespa ...

  3. 2440 lcd10分钟休眠修改

    在我们的系统中,LCD的虚拟控制台和控制台TTY不是同一个设备,也就是说,如果在程序里单纯的printf是不行的!这样只能修改你正在使用的TTY的blankinterval,而你用的却是文本方式的设备 ...

  4. $where $options: 'g','i'

    db.classes.update({"count":{$gt:20}},{$set:{"name":"c4"}},false,false) ...

  5. [开发笔记]-WindowsService服务程序开发

    Windows服务:Microsoft Windows 服务(即,以前的 NT服务)使您能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可 ...

  6. HDU 2594 扩展kmp模板题

    题目大意: 给定两个字符串,在第一个字符串中找到一个最大前缀作为第二个字符串的后缀 #include <iostream> #include <cstdio> #include ...

  7. 反Secure Boot垄断:兼谈如何在Windows 8电脑上安装Linux

    感谢HQSQ的投递一.自由软件基金会的呼吁上周,2012年将近结束的时候,自由软件基金会(FSF)发出呼吁,要求人们继续支持反Secure Boot垄断,希望签名者能达到5万人(目前是4万).我觉得, ...

  8. Linux摄像头驱动学习之:(二)通过虚拟驱动vivi分析摄像头驱动

    一.通过指令 "strace -o xawtv.log xawtv" 得到以下调用信息:// 1~7都是在v4l2_open里调用1. open2. ioctl(4, VIDIOC ...

  9. java基础之 溢出

    堆溢出 堆(Heap)是Java存放对象实例的地方. 堆溢出可以分为以下两种情况,这两种情况都会抛出OutOfMemoryError:java heap space异常: 1.内存泄漏 内存泄漏是指对 ...

  10. [安卓]softap

    http://www.cnblogs.com/javawebsoa/archive/2013/05/29/3106758.html