[抄题]:

Given a non-empty binary tree, return the average value of the nodes on each level in the form of an array.

Example 1:

Input:
3
/ \
9 20
/ \
15 7
Output: [3, 14.5, 11]
Explanation:
The average value of nodes on level 0 is 3, on level 1 is 14.5, and on level 2 is 11. Hence return [3, 14.5, 11].

[暴力解法]:

时间分析:

空间分析:

[奇葩输出条件]:

[奇葩corner case]:

[思维问题]:

BFS写不熟

[一句话思路]:

(3先生)先加头、先判Empty、先取长度

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. 没概念:BFS中的for循环是针对当前这一层的操作,add的元素都属于下一层
  2. 二叉树层遍历,q中存的是节点,记住就行了

[二刷]:

[三刷]:

[四刷]:

[五刷]:

[五分钟肉眼debug的结果]:

[总结]:

BFS里既然取了长度,就用在for循环中

[复杂度]:Time complexity: O(n) Space complexity: O(n)

所有点放进去一次,拿出来一次,最终还是n

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[关键模板化代码]:

q.offer(root);
//isEmpty()
while (! q.isEmpty()) {
//get length
int n = q.size();

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

[代码风格] :

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
//corner case
List<Double> ans = new ArrayList<Double>();
Queue<TreeNode> q = new LinkedList<TreeNode>(); if (root == null) {
return ans;
}
//bfs
//offer root
q.offer(root);
//isEmpty()
while (! q.isEmpty()) {
//get length
int n = q.size();
double sum = 0.0;
for (int i = 0; i < n; i++) {
TreeNode node = q.poll();
sum += node.val;
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
ans.add(sum / n);
}
//return
return ans;
}
}

637. Average of Levels in Binary Tree 二叉树的层次遍历再求均值的更多相关文章

  1. LeetCode 637. Average of Levels in Binary Tree二叉树的层平均值 (C++)

    题目: Given a non-empty binary tree, return the average value of the nodes on each level in the form o ...

  2. [LeetCode] 637. Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  3. 【Leetcode_easy】637. Average of Levels in Binary Tree

    problem 637. Average of Levels in Binary Tree 参考 1. Leetcode_easy_637. Average of Levels in Binary T ...

  4. 637. Average of Levels in Binary Tree - LeetCode

    Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...

  5. 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  6. [LeetCode] Average of Levels in Binary Tree 二叉树的层平均值

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  7. LeetCode 637 Average of Levels in Binary Tree 解题报告

    题目要求 Given a non-empty binary tree, return the average value of the nodes on each level in the form ...

  8. 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...

  9. LeetCode - 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

随机推荐

  1. python-unittest-生成测试报告

    HTMLTestRunner HTMLTestRunner 是 Python 标准库的 unittest 单元测试框架的一个扩展.它生成易于使用的 HTML 测试报告. 一.目录结构 先来看一下项目的 ...

  2. Regexper:牛逼的 JavaScript 正则可视化工具

    RequireJS Optimizer 是 RequireJS 自带的前端优化工具,可以对 RequireJS 项目中的 JavaScript & CSS 代码使用 UglifyJS 或者 C ...

  3. xss 攻击 sql 注入

    XSS测试 "/><script>alert(document.cookie)</script><!-- <script>alert(docu ...

  4. java代码----I/O流写出整型,浮点型,

    总结: package com.a.b; import java.io.*; public class fdsf { public static void main(String[] args) th ...

  5. laravel 环境自编译过程

    [原创] 看到此文的朋友看完后也许会失望,但我尽最大努力不让搜友们失望,以下是自己操作的笔记用以整理提高 虽然 laravel 官方已给出了安装 laravel 框架所需的环境盒子 使用Vagrant ...

  6. 阿里云中域名的MX记录添加方法

    如何添加阿里云的MX记录 1. 登录阿里云,点击“云解析”,点击自己想要添加MX记录的域名: 2. 点击新手引导设置: 3. 点击解析设置,自动跳出“设置网站”和“设置邮箱”: 4. 跳出的页面,选择 ...

  7. ERROR无法从静态上下文中引用非静态变量

    ERROR无法从静态上下文中引用非静态变量 2012-06-16 20:58:52 分类: Java 什么是“static”? 学习过java.C++或C的人都应该认识这个关键字.用这个关键字修饰的变 ...

  8. WordSmith2013-7-31

    WordSmith Good Evening Ladies and Gentlemen,I’am Jason,I’m pleasured  to be wordsmith tonight. First ...

  9. Oracle导出CSV文件

    -- 建立存储过程 CREATE OR REPLACE PROCEDURE SQL_TO_CSV ( P_QUERY IN VARCHAR2, -- PLSQL文 P_DIR IN VARCHAR2, ...

  10. Network Real Trace Analysis 2015年12月10日

    了解网络中真实的流量,国内很难找到巨人的肩膀. WAND是新西兰waikato 大学计算机系的研究小组,主要做网络测量,大规模网络流量捕获,网络分析.还做专业的分析软件. libtrace是其开源的分 ...