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 ...
随机推荐
- linux -- "./configure --prefix "命令
源码的安装一般由3个步骤组成:配置(configure).编译(make).安装(make install). Configure是一个可执行脚本,它有很多选项,在待安装的源码路径下使用命令./con ...
- 如何使用Matrix对bitmap的旋转与镜像水平垂直翻转
Bitmap convert(Bitmap a, int width, int height){int w = a.getWidth();int h = a.getHeight();Bitmap ne ...
- linux网络配置练习
查看网卡是否正常安装 命令:lspci |grep Ether 1.修改网卡配置 命令: vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth ...
- java 项目 存入mysql后 变问号 MySql 5.6 (X64) 解压版 1067错误与编码问题的解决方案
[参考]MySQL 5.7.19 忘记密码 重置密码 my.ini示例 服务启动后停止 环境 Java环境JDK1.8 安装好了 mysql-5.6.38-winx64 idea2016(64) ...
- Mongodb学习笔记(2)--修改器
修改器 利用原子的更新修改器,可以使得这种部分更新极为高效,更新修改器是一种特殊的键,用来指定复杂的更新操作,比如调整,增加或删除,还可以操作数组或内嵌文档. $inc $inc修改器用来增加已有键的 ...
- 184使用 Core Image 框架处理照片
实现图片的老旧.色彩.旋转效果 (1)使用 StoryBoard 故事版布局界面: (2)使用 Core Image 框架的 CIFilter 过滤器:分别对应的过滤器名称是:CISepiaTone( ...
- WWDC 2015大会到来了
WWDC 2015大会到来了,观看到凌晨3点,困死了. 从现场直播视频可以看到: (1)iOS 9的新体验:Siri更智能.Search更全面.苹果支付更方便.Notes和News更新颖好用.地图应用 ...
- 【Deep Learning】RNN LSTM 推导
http://blog.csdn.net/Dark_Scope/article/details/47056361 http://blog.csdn.net/hongmaodaxia/article/d ...
- 开发kendo-ui弹窗组件
摘要: kendo-ui中只是提供了windwo插件,并没有提供页内弹窗插件.现在分享项目中自己定制的基于window组件的弹窗插件,如果你的项目也是用的kendo-ui,只需要将组件代码引到项目中即 ...
- VMWare------启动虚拟机时出现“start booting fron CD... Error loading image:DFEAULT.EZB”提示
提示详情: start booting fron CD... Error loading image:DFEAULT.EZB 解决方法: iso镜像文件有问题,需要下载正确的镜像文件 MSDN下载地址 ...