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 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:
- 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的更多相关文章
- [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 ...
- 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 ...
- 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 ...
- 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 ...
- 637. Average of Levels in Binary Tree - LeetCode
Question 637. Average of Levels in Binary Tree Solution 思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这 ...
- 【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 ...
- 【LeetCode】637. Average of Levels in Binary Tree 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:DFS 方法二:BFS 日期 题目地址:ht ...
- [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 ...
- 【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 ...
随机推荐
- ubuntu -- 不输入密码执行sudo
作为ubuntu等桌面系统,默认登录的帐号是没有root权限的,为了提升权限来执行任务,我们一般用到 "sudo+命令" 来执行,但是不难发现我们 一般都要输入密码.那么有没有什么 ...
- 由于PADT伪造攻击带来的大面积掉线原因分析
今天一早接到一个客户电话,说他有一个交换机下面的用户,大面积和上线下线. 由于之有已建议用户在主干换了普通VLAN交换机.所以这次出现问题概率较小,只在一条支路的交换机下面. 下面我对这个情况的发生做 ...
- SQLSERVER 2008 技术内幕 T-SQL查询 笔记1: SQL 执行顺序
与大多数语言一样,SQL语言也有一个执行顺序,只是在大多数编程语言中,代码是按照编写顺序来处理的,而在SQL中则不是,下图为SQL 执行顺序. () ) [ ALL | DISTINCT ] () [ ...
- 【项目管理】Project使用
http://www.cnblogs.com/wangfupeng1988/p/3647166.html
- 怎么用ABBYY在线浏览PDF文件
ABBYY FineReader 让您可以从在线存储服务中打开图像或 PDF 文件,并将已识别文本保存至在线存储服务中,如 Dropbox.SkyDrive 或 Google Drive 等.通过在 ...
- java的final关键字——修饰变量
final修饰的变量不可变,指的是引用不可变,(除基本类型)而不是内容. final修饰的成员变量必须被初始化
- python 类属性 、实例属性,可变数据结构作为类属性需要注意的地方
1.一些经典的python错误不去阅读和不重视,就会把错误的做法带入到实际项目中来,甚至造成难以排查问题. 2.有一个大笨猪,按java写观察者模式,java写得是直接在类名下声明一个实例属性(不加s ...
- Android输出日志到电脑磁盘
使用Eclipse查看Log有时候挺恶心的,有些Log ADB会自动的清除,所有有时候导致抓不到有效的Log,把Log保存到文件,然后通过文本查看器查看,感觉好Happy,下面就是脚本文件: adb ...
- Linux+Redis实战教程_day01_Linux系统上安装tomcat
Linux系统上安装tomcat 安装tomcat 上传tomcat的安装文件 Alt+p 拖拽上传 创建tomcat的安装路径 mkdir -p /usr/local/tomcat 解压tomcat ...
- Dubbo -- 系统学习 笔记 -- 示例 -- 服务分组
Dubbo -- 系统学习 笔记 -- 目录 示例 想完整的运行起来,请参见:快速启动,这里只列出各种场景的配置方式 服务分组 当一个接口有多种实现时,可以用group区分. <dubbo:se ...