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. jmeter非GUI界面常用参数详解

    压力测试或者接口自动化测试常常用到的jmeter非GUI参数,以下记录作为以后的参考 讲解:非GUI界面,压测参数讲解(欢迎加入QQ群一起讨论性能测试:537188253) -h 帮助 -n 非GUI ...

  2. Leetcode42. 接雨水

    42. 接雨水 做法 考虑单独一列能生产多少贡献:用左右最大值去接这列的水 \(O(n)\) Code class Solution { public: int mx[1000000],rx[1000 ...

  3. video标签在浏览器不能使用的问题 ?

    之前video标签是可以用的,但是压缩之后在移动端可以用,在pc浏览器不可以用? 怎么解决? 这样的写法会报错  说是缺乏source  但是你加上source也没有用 <video src=& ...

  4. kafka集群搭建(图文并用)

    将安装包上传服务器并解压 scp kafka_2.11-1.0.0.tgz username@{ip}:~/. mkdir /usr/local/kafka mv kafka_2.11-1.0.0.t ...

  5. etcd,flannel,docker relationship---and k8s

    journalctl -xe voidcn.com/article/p-qufvdmpq-bqn.html etcd more etcd.confETCD_NAME=default ETCD_DATA ...

  6. SQL学习笔记(三)

    左连接 格式:select * from 表1 left join 表2 on 表1.列=表2.列 例1:查询所有学生的成绩,包括没有成绩的学生. 例2:查询所有学生的成绩,包括没有成绩的学生,需要显 ...

  7. nodejs五子棋online游戏开发视频教程,客户端cocos creator js

    开发的游戏是五子棋online,网络版的,服务端部分和客户端部分都在这个教程里面,可以看一下目录! 服务器nodejs游戏开发教程 使用Nodejs开发网络服务器 游戏服务端 ,cocos creat ...

  8. $objPHPExcel=$objReader->load() 报错路径不存在

    PHPexcel导入excel内容到数据库出错, $objPHPExcel=$objReader->load()报错 Could not open /public/upload/20191028 ...

  9. dubbo线程模型配置

    首先了解一下dubbo线程模型 如果事件处理的逻辑能迅速完成,并且不会发起新的IO请求,比如只是在内存中记个标识.则直接在IO线程上处理更快,因为减少了线程池调度. 但如果事件处理逻辑较慢,或者需要发 ...

  10. flutter 运行别人项目 包无法导入报错:Target of URI doesn't exist 'package:flutter/material.dart' 解决方法

    命令行里运行  flutter packages get