-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage-of-levels-in-binary-tree.js
59 lines (51 loc) · 1.22 KB
/
average-of-levels-in-binary-tree.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
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* 二叉树的层平均值
* @param {TreeNode} root
* @return {number[]}
*/
var averageOfLevels = function (root) {
// 1. DFS
let result = [];
const dfs = (node, level = 0) => {
if (node) {
if (!result[level]) result[level] = [];
result[level].push(node.val);
dfs(node.left, level + 1);
dfs(node.right, level + 1);
}
};
dfs(root);
result = result.map((item) => {
const sum = item.reduce((a, b) => a + b);
return Number((sum / item.length).toFixed(5));
});
return result;
// 2. BFS
// return bfs(root);
};
function bfs(root) {
const result = [];
const queue = [];
queue.push(root);
while (queue.length > 0) {
const len = queue.length;
let size = len;
let sum = 0;
while (size--) {
const node = queue.shift();
sum += node.val;
node.left && queue.push(node.left);
node.right && queue.push(node.right);
}
result.push(Number((sum / len).toFixed(5)));
}
return result;
}