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. solr集群搭建,zookeeper集群管理

    1. 第一步 把solrhome中的配置文件上传到zookeeper集群.使用zookeeper的客户端上传. 客户端命令位置:/root/solr-4.10.3/example/scripts/cl ...

  2. 关于elementUI如何在表格循环列表里分别新增Tag的设计使用

    话不多说,直接上代码.想要实现的目的是这样的,想要在表格里单独添加每一个tag 那么,需要解决的问题就是如何定义这每一个插槽里的输入框,把每个输入框以及里面插入的数据区分开. 研究了很久,最后选择了对 ...

  3. simulink中的两种求导模块

    最终图像都是下图形式

  4. Day 19: EmberJS 入门指南

    编者注:我们发现了有趣的系列文章<30天学习30种新技术>,正在翻译,一天一篇更新,年终礼包.下面是第19天的内容. 到目前为止,我们这一系列文章涉及了Bower.AngularJS.Gr ...

  5. stylus css tooltips 工具提示

    tooltips 纯css工具提示 bubbles-tooltips 查看效果 演示 安装 npm install tooltips --save 使用 在 gulp 中使用 gulp var gul ...

  6. 关于Css的垂直居中的一些方法

    前两种方法称为大致居中,一般误差随高度的减小而减小,不过一般来说不怎么看得出来,除非你用javascript调用offsetTop来查看.不然没有强迫症的比较难看出来.但是兼容性很好,尤其是table ...

  7. 前端面试题整理——HTML/CSS

    如何理解语义化: 对应的内容是用相应意思的标签,增加开发者和机器爬虫对代码的可读性. 块状元素和内联元素: 块状元素有:display:block/table:有div h1 h2 table ul  ...

  8. 【Android开发】【布局】几个常用布局构成的简单demo

    图image1.jpg,就是常用的 底部菜单栏 + Fragment联动 使用 RadioGroup + Fragment 图image2.jpg ,就是 TabLayout + ViewPager ...

  9. 多条命令同时执行的包concurrently

    npm i concurrently use "script":{ "client:build": "webpack --config build/w ...

  10. CommonsCollection7反序列化链学习

    CommonsCollections7 1.前置知识 Hashtable Hashtable实现了Map接口和Serializable接口,因此,Hashtable现在集成到了集合框架中.它和Hash ...