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 -- 安装最新版的nodejs
1.安装最新的nodejs和npm # apt-get update # apt-get install -y python-software-properties software-properti ...
- 利用jQuery实现回收站删除效果
jQuery是一款非常强大的Javascript脚本库,我们开发者喜欢jQuery的原因除了它代码简洁外,更多的是因为jQuery插件非常丰富.今天我们用一个示例来解说jQuery是如何实现拖拽的. ...
- Throwable vs Exception
Throwable中的Error是不需要程序处理的. Exception是需要处理的.
- scala实现相邻两个元素挑换位置的代码,哈哈
import scala.math._ import breeze.plot._ import breeze.linalg._ import scala.collection.mutable.Arra ...
- lnmp手动新建虚拟机
1.在home/wwwroot/zhongjie 新建zhongjie文件夹 2.在usr/local/nginx/conf/vhost/zhongjie.conf 新建配置文件zhongj ...
- vue-router2路由参数注意问题
1.vue 路由 如果传递 params 定义路由的时候是 /路由名称:id 获取的时候 this.$route.params.id 最后形如 /路由名称/路由参数 传参的时候 params:{ st ...
- mysql 两台主主复制配置
A.服务器 [mysqld] # The TCP/IP Port the MySQL Server will listen on port= server-id= #master-host=10.1. ...
- tooltips插件
摘要: 继‘带箭头提示框’,本文将分享几款带箭头提示框. qtipqTip是一种先进的提示插件,基于jQuery框架.以用户友好,而且功能丰富,qTip为您提供不一般的功能,如圆角和语音气泡提示,并且 ...
- it码农之心灵鸡汤(一)
到底该怎么面对工作,到底怎么面临人生.到底怎么面临青春,对于打工的人来说这些一直都是心中一直无法解惑的谜团. 对于人们怎样看待工作,以前华为创始人任正非说过:非常多人问我,来公司工作有没有双休?需不须 ...
- Java实现匿名内部类的简单应用
在查看数码相片时,通常会使用一款图片查看软件,该软件应该能遍历文件夹下的所有图片并进行显示.编写程序,实现一个图片查看软件,它可以支持6张图片,通过单击不同的按钮就可以查看不同的图片. 思路分析:就是 ...