# 637. Average of Levels in Binary Tree - LeetCode 原

yysue

## Solution

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;
}
}
}
``````

© 著作权归作者所有

### yysue

LeetCode笔记：637. Average of Levels in Binary Tree

Cloudox_
01/11
0
0
[leetcode]Binary Search Tree Iterator

question Implement an iterator over a binary search tree (BST). Your iterator will be initialized with the root node of a BST. Calling will return the next smallest number in th......

NineRec
2015/08/27
0
0
[LeetCode] Maximum Width of Binary Tree 二叉树的最大宽度

Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structu......

2017/12/06
0
0
[LeetCode] Binary Tree Level Order Traversal

★ 题目 https://leetcode.com/problems/binary-tree-level-order-traversal/description/ Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left......

u013553529
01/13
0
0
[LeetCode] Second Minimum Node In a Binary Tree

[LeetCode] Second Minimum Node In a Binary Tree 题目 https://leetcode.com/problems/second-minimum-node-in-a-binary-tree/ Given a non-empty special binary tree consisting of node......

u013553529
2017/11/25
0
0
leetcode -- Maximum Depth of Binary Tree

Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest......

leiline
01/12
0
0
leetcode -- Balanced Binary Tree

Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of t......

leiline
01/12
0
0
LeetCode：Invert Binary Tree - 反转二叉树

1、题目名称 Invert Binary Tree（反转二叉树） 2、题目地址 https://leetcode.com/problems/invert-binary-tree/ 3、题目内容 英文： Invert a binary tree. to 中文：反转一颗二叉树。 4、...

2016/02/11
483
1
Binary Tree Traversal - two styles of solutions

s360564346
2016/11/25
8
0
【刷题】Lowest Common Ancestor

2017/12/28
0
0

3分钟前
0
0
rabbitmq学习记录（五）交换机Exchange-fanout

24分钟前
0
0
sqoop导入数据到Base并同步hive与impala

hblt-j
30分钟前
0
0
Dart 服务端开发 文件上传

clent端使用angular组件 upload_component.html form id="myForm" method="POST" enctype="multipart/form-data"> <input type="file" name="fileData"> <!-- file field --></form>......

scooplol
30分钟前
0
0
apache和tomcat同时开启，乱码问题

tomcat和apache同时开启，会走apache的转发，执行的是AJP/1.3协议。所以在tomcat的配置文件server中, <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" useBodyEncodingForU......

Kefy
47分钟前
0
0

ssh-keygen 产生公钥与私钥对. ssh-copy-id 将本机的公钥复制到远程机器的authorized_keys文件中，ssh-copy-id也能让你有到远程机器的home, ~./ssh , 和 ~/.ssh/authorized_keys的权利 第一步...

xtof

0
0
orcale 查询表结构

SELECT t.table_name, t.colUMN_NAME, t.DATA_TYPE || '(' || t.DATA_LENGTH || ')', t1.COMMENTS FROM User_Tab_Cols t, User_Col_Comments t1WHERE t.table_name......

wertwang

0
0
Java 之 反射

0
0

0
0

yeahlife

0
0