-
Notifications
You must be signed in to change notification settings - Fork 0
/
637_AverageOfLevelsInBinaryTree.js
59 lines (48 loc) · 1.16 KB
/
637_AverageOfLevelsInBinaryTree.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// https://leetcode.com/problems/average-of-levels-in-binary-tree/#/description
/**
* Definition for a binary tree node.
* function TreeNode(val) {
* this.val = val;
* this.left = this.right = null;
* }
*/
function TreeNode(val) {
this.val = val;
this.left = this.right = null;
}
/**
* @param {TreeNode} root
* @return {number[]}
*/
var averageOfLevels = function (root) {
var obj = {};
var traverse = function (node, depth) {
if (!node) return;
if (obj[depth] === undefined) {
obj[depth] = {
sum: 0,
count: 0
};
}
obj[depth].sum += node.val;
obj[depth].count++;
depth++;
traverse(node.left, depth);
traverse(node.right, depth);
}
traverse(root, 0);
var keys = Object.keys(obj);
var result = [];
for (var i = 0; i < keys.length; i++) {
result[i] = obj[keys[i]].sum / obj[keys[i]].count;
}
return result;
};
var root = new TreeNode(3);
root.left = new TreeNode(9);
root.right = new TreeNode(20);
root.right.left = new TreeNode(15);
root.right.right = new TreeNode(7);
console.log(averageOfLevels(root));
root.left.left = new TreeNode(4);
console.log(averageOfLevels(root));