637. 二叉树的层平均值

637. Average of Levels in Binary Tree

LeetCode637. Average of Levels in Binary Tree

题目描述

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组.

示例 1:

输入:

    3
/ \
9 20
/ \
15 7

输出: [3, 14.5, 11]

解释:

第 0 层的平均值是 3,第 1 层是 14.5,第 2 层是 11.因此返回 [3, 14.5, 11].

注意:

1. 节点值的范围在32位有符号整数范围内。

Java 实现

import java.util.LinkedList;
import java.util.List;
import java.util.Queue; class TreeNode {
int val;
TreeNode left;
TreeNode right; TreeNode(int x) {
val = x;
}
} class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> result = new LinkedList<>();
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) {
return result;
}
queue.offer(root);
while (!queue.isEmpty()) {
double avg = 0.0;
int size = queue.size();
for (int i = 0; i < size; i++) {
if (queue.peek().left != null) {
queue.offer(queue.peek().left);
}
if (queue.peek().right != null) {
queue.offer(queue.peek().right);
}
avg += queue.poll().val;
}
result.add(avg / size);
}
return result;
}
}

相似题目

参考资料

LeetCode 637. 二叉树的层平均值(Average of Levels in Binary Tree)的更多相关文章

  1. [Swift]LeetCode637. 二叉树的层平均值 | 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. 二叉树的层平均值

    Leetcode:637. 二叉树的层平均值 Leetcode:637. 二叉树的层平均值 Talk is cheap . Show me the code . /** * Definition fo ...

  3. Java实现 LeetCode 637 二叉树的层平均值(遍历树)

    637. 二叉树的层平均值 给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7 输出: [3, 14.5, 11] 解释: 第0层的 ...

  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二叉树的层平均值 (C++)

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

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

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

  9. LeetCode算法题-Average of Levels in Binary Tree(Java实现)

    这是悦乐书的第277次更新,第293篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第145题(顺位题号是637).给定一个非空二叉树,以数组的形式返回每一层节点值之和的平 ...

随机推荐

  1. Ring3挂起进程,跟恢复进程.

    目录 Ring3挂起进程,跟恢复进程. 一丶简介 二丶代码 Ring3挂起进程,跟恢复进程. 一丶简介 有时候我们做对抗的时候可能会遇到.一个进程常常操作我们.但是我们 可以通过挂起进程来挂起它让它无 ...

  2. Kafka与ActiveMQ区别

    Kafka 是LinkedIn 开发的一个高性能.分布式的消息系统,广泛用于日志收集.流式数据处理.在线和离线消息分发等场景.虽然不是作为传统的MQ来设计,在大部分情况,Kafaka 也可以代替原先A ...

  3. 23种C#设计模式,源码在GitHub ( 具体代码 , 优缺点 , 相关网址) 希望对大家有所帮助

    点击 进入Github 地址

  4. VS2019输出信息到调试控制台

    System.Diagnostics.Debug.WriteLine(format, args);

  5. 启动tomcat闪退

    启动tomcat,点击startup.bat闪退 使用的Tomcat是免安装版本的. 因为在启动tomcat是需要读取环境变量和配置信息,缺少了这些信息,就不能登记环境变量,导致了tomcat的闪退. ...

  6. vue+vue-resource设置请求头(带上token)

    前言 有这样的一个需求,后台服务器要求把token放在请求头里面 嗯一般是通过data里面通过参数带过去的 第一种方法 全局改变: Vue.http.headers.common['token'] = ...

  7. Barman安装及备份PostgreSQL

    barman特点 零数据丢失备份.保证用户在只有一台备份服务器的情况下达到零数据丢失. 与备份服务器合作.允许备份服务器在与主服务器的流式复制不可用时,从barman获取wal文件. 可靠的监控集成. ...

  8. 利用JS-SDK微信分享接口调用(后端.NET)

    一直都想研究一下JS-SDK微信分享的接口调用,由于最近工作需要,研究了一下,目前只是实现了部分接口的调用:其他接口调用也是类似的: 在开发之前,需要提前准备一个微信公众号,并且域名JSAPI 配置接 ...

  9. JVM 扩展类加载器2

    1.创建Sample public class MyTest22 { static { System.out.println("MyTest22 initializer"); } ...

  10. Java hashCode与equals学习

    1.关于Object类的equals方法的特点 a) 自反性: x.equals(x) 应该返回true b) 对称性: x.equals(y)为true,那么y.equals(x) 也为true c ...