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

Note:

  1. The range of node's value is in the range of 32-bit signed integer.

按层遍历,求每层平均值。

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
import java.util.LinkedList;
import java.util.Queue;
public class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> retList = new ArrayList<Double>();
if (root == null)
return retList; Queue<TreeNode> q = new LinkedList<TreeNode>();
q.offer(root); while ( ! q.isEmpty() ) {
List<TreeNode> tl = new ArrayList<TreeNode>();
double sum = 0;
while ( ! q.isEmpty() ) {
TreeNode curr = q.poll();
tl.add(curr);
sum += (double) curr.val;
}
retList.add(sum / tl.size());
for (TreeNode n : tl) {
if (n.left != null)
q.offer(n.left);
if (n.right != null)
q.offer(n.right);
}
}
return retList;
}
}
 

LeetCode - 637. Average of Levels in Binary Tree的更多相关文章

  1. [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 ...

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

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

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

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

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

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

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

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

  8. [LeetCode&Python] Problem 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 ...

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

随机推荐

  1. webstorm 重置所有设置

    我的是win10的,删除如下路径的文件夹C:\Users\XXX(你自己电脑用户名)\.IntelliJIdeaxxxx(版本号) 这是最快捷的方法

  2. 长姿势 教你在qq空间上显示iPhone6尾巴

    下午刚午休完的时候,广州很多童鞋都感受到了震感,半青也感受到了,不仅如此,我还感受到了更大震感,那就是翻一下QQ空间动态,竟然看到有一位好友的尾巴竟然显示为“iPhone6”,顿时觉得该好友逼格太高了 ...

  3. 【Intel AF 2.1 学习笔记二】AF中的页面——Panel

    Panel Panel控件是你的app中的独立内容的区域控件.它是af UI的核心.Panel div 元素实际上承载了app中你管理和显示的界面元素和内容. 创建panel控件是相当地容易的:在id ...

  4. greenplum的用法

     gp建表的实例         gp 创建外部表的实例:(外部表不能建立分布键)              CREATE EXTERNAL TABLE user_app_tag (         ...

  5. Android学习之——SpannableString和TextView的使用及研究

    前言 相信大家对Android的TextView的使用已经相当熟悉了,但有没有发现TextView的文字总是那么单调,可以修改的就字体大小,颜色等等.要想实现自定义的文字,就要用到我们今天的主角--S ...

  6. ESPCN超分辨率汇总

    Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural ...

  7. kendo-ui的MVVM模式

    摘要: MVVM(Model View ViewModel)是一种帮助开发者将数据从模型分离的设计模式.MVVM的ViewModel负责将数据对象从模型中分离出来,通过这种方式数据就很容易控制数据如何 ...

  8. javascript的特殊条件语句

    摘要: ​ 由于javascript语言的特殊性导致它有很多特殊的条件判断,下面我列出了一些特殊的条件判断语句和他们对应的结果. if(condition) { console.log(true); ...

  9. 关于 wsdl2Java 自动生成客户端调取webservice接口

    webservice地址:http://www.webxml.com.cn/WebServices/WeatherWebService.asmx?wsdl wsdl2Java 自动生成类名: 客户端调 ...

  10. [Converge] Backpropagation Algorithm

    Ref: CS231n Winter 2016: Lecture 4: Backpropagation Ref: How to implement a NN:中文翻译版本 Ref: Jacobian矩 ...