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. 判断是否是iOS8

    判断是否是iOS8 //判断是否是iOS8 if ([[UIDevice currentDevice].systemVersion doubleValue]>=8.0) { NSLog(@&qu ...

  2. C#窗体的加载等待(BackgroundWorker控件)实现

    窗体拉一个Button按钮和一个加载等待显示的label, label默认隐藏,点击按钮时显示这个label,加载完再隐藏 1.工具箱拉BackgroundWorker控件到窗体 2.backgrou ...

  3. warning malformed '#pragma pack(push[, id], n)' - ignored

    bmp.c:8: warning: malformed '#pragma pack(push[, id], <n>)' - ignored bmp.c:33: warning: #prag ...

  4. BroadCastReceiver相关知识--读书笔记

    2013-12-30 16:55:07 1. BroadCastReceiver是Android四大组件之一,本质上是一个系统级的监视器. 2. 每次BroadCast事件发生后,系统都会创建对应的B ...

  5. JS获取上传文件的名称、格式、大小

    <input id="File1" type="file" onchange="checkFile(this)" /> 方式一) ...

  6. matlab 画框(三) 画框并保存图像

    initstate = [x y w h];%-----------------------------------------Show the tracking resultimshow(uint8 ...

  7. 创建MySQL数据库和表(一)

    一.启动MySQL服务 1.在Windows操作系统的“服务”中启动,找到你安装MySQL的起的服务名称,我本机服务名的是MySQL. 2.在命令行中用命令启动: A.启动MySQL服务:net st ...

  8. hadoop启动jobhistoryserver

    hadoop启动jobhistoryserver来实现web查看作业的历史运行情况,由于在启动hdfs和Yarn进程之后,jobhistoryserver进程并没有启动,需要手动启动,启动的方法是通过 ...

  9. 关于wait和notify的用法

    通常,多线程之间需要协调工作.例如,浏览器的一个显示图片的线程displayThread想要执行显示图片的任务,必须等待下载线程 downloadThread将该图片下载完毕.如果图片还没有下载完,d ...

  10. Repeater分页

    void BindData()        {            PagedDataSource pds = new PagedDataSource();                     ...