Question

637. Average of Levels in Binary Tree

Solution

思路:定义一个map,层数作为key,value保存每层的元素个数和所有元素的和,遍历这个树,把map里面填值,遍历结束后,再遍历这个map,把每层的平均数放到数组里,最后数组转为list返回,不用考虑list里的排序了.

Java实现:

/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<Double> averageOfLevels(TreeNode root) { Map<Integer, TreeLevel> store = new HashMap<>();
init(root, store, 1);
Double[] arr = new Double[store.size()];
for (Map.Entry<Integer, TreeLevel> entry : store.entrySet()) {
arr[entry.getKey() - 1] = entry.getValue().getAverage();
}
return Arrays.asList(arr);
} void init(TreeNode root, Map<Integer, TreeLevel> store, int level) {
if (root == null) return;
TreeLevel treeLevel = store.get(level);
if (treeLevel == null) {
treeLevel = new TreeLevel();
store.put(level, treeLevel);
}
treeLevel.count++;
treeLevel.sum+=root.val;
init(root.left, store, level+1);
init(root.right, store, level+1);
} // 定义一个类存储每一层的信息
class TreeLevel {
int count; // 该层有多少元素
double sum; // 该层所有元素的和,这个要用double类型来保存,eg:[2147483647,2147483647,2147483647] // 返回该层的平均数
Double getAverage() {
return sum / count;
}
}
}

637. Average of Levels in Binary Tree - LeetCode的更多相关文章

  1. 【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 ...

  2. [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 ...

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

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

  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 ...

  5. 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 ...

  6. 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 ...

  7. [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 ...

  8. 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 ...

  9. 【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 ...

随机推荐

  1. (2)_引言Introduction【论文写作】

  2. 前端架构gulp与webpack(知识点整理)

    一 概念介绍 gulp 是 task runner,Webpack 是 module bundler.可以这么说, Webpack 和 gulp 本身都有 95% 的功能是不能被对方替代,或者直接说和 ...

  3. html dom 转化成图片踩坑记(canvas toDataURL)

    需求 在开发过程中遇到这么一个需求,h5页面需要将一个html dom转化成图片,便于用户保存. 面向百度搜索第三方得 html2canvas 和 dom-to-image 两者在写这篇笔记之前在gi ...

  4. 如何使用Flannel搭建跨主机互联的容器网络

    当您将多台服务器节点组成一个Docker集群时,需要对集群网络进行设置,否则默认情况下,无法跨主机容器互联,接下来我们首先分析一下原因. 跨主机容器互联 下图描述了一个简单的集群网络,在该集群内,有两 ...

  5. JS 用状态机的思想看Generator之基本语法篇

    前言 最近学习了阮一峰老师的<ECMAScript 6 入门>里的Generator相关知识,以及<你不知道的JS>中卷的异步编程部分.同时在SegmentFault问答区看到 ...

  6. .NET程序设计实验二

    实验二  面向对象程序设计 一.实验目的 1. 理解类的定义.继承等面向对象的的基本概念: 2. 掌握C#语言定义类及其各种成员(字段,属性,方法)的方法: 3. 掌握方法覆盖的应用: 4. 掌握接口 ...

  7. 微信小程序列表拖动排序Demo

    wxml页面编写 <view class="container"> <view bindtap="box" class="box&q ...

  8. 使用docker安装centos6.10镜像并安装新版gcc

    使用docker安装centos6.10镜像并安装新版gcc 环境:Linux Ubuntu 16.04.7 LTS 目录 使用docker安装centos6.10镜像并安装新版gcc 使用docke ...

  9. 编译python(cpython)的源码及其用途

    获取python的源码 3.x及最新版本的源码:https://github.com/python/cpython python2.7分支的源码:https://github.com/python/c ...

  10. Go 1.18泛型的局限性初探

    前言 Go 1.18 版本之后正式引入泛型,它被称作类型参数(type parameters),本文初步介绍 Go 中泛型的使用.长期以来 go 都没有泛型的概念,只有接口 interface 偶尔类 ...