Skip to content

Commit 043bcf9

Browse files
authored
feat: add javascript solution to lc problem: No.0637.Average of Levels in Binary Tree (doocs#401)
1 parent 29928e9 commit 043bcf9

File tree

3 files changed

+106
-0
lines changed

3 files changed

+106
-0
lines changed

solution/0600-0699/0637.Average of Levels in Binary Tree/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,43 @@ class Solution {
111111
}
112112
```
113113

114+
### **JavaScript**
115+
116+
```js
117+
/**
118+
* Definition for a binary tree node.
119+
* function TreeNode(val, left, right) {
120+
* this.val = (val===undefined ? 0 : val)
121+
* this.left = (left===undefined ? null : left)
122+
* this.right = (right===undefined ? null : right)
123+
* }
124+
*/
125+
/**
126+
* @param {TreeNode} root
127+
* @return {number[]}
128+
*/
129+
var averageOfLevels = function(root) {
130+
let res = [];
131+
let queue = [root];
132+
while (queue.length > 0) {
133+
n = queue.length;
134+
let sum = 0;
135+
for (let i = 0; i < n; i++) {
136+
let node = queue.shift();
137+
sum += node.val;
138+
if (node.left) {
139+
queue.push(node.left);
140+
}
141+
if (node.right) {
142+
queue.push(node.right);
143+
}
144+
}
145+
res.push(sum / n);
146+
}
147+
return res;
148+
};
149+
```
150+
114151
### **...**
115152

116153
```

solution/0600-0699/0637.Average of Levels in Binary Tree/README_EN.md

+37
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,43 @@ class Solution {
104104
}
105105
```
106106

107+
### **JavaScript**
108+
109+
```js
110+
/**
111+
* Definition for a binary tree node.
112+
* function TreeNode(val, left, right) {
113+
* this.val = (val===undefined ? 0 : val)
114+
* this.left = (left===undefined ? null : left)
115+
* this.right = (right===undefined ? null : right)
116+
* }
117+
*/
118+
/**
119+
* @param {TreeNode} root
120+
* @return {number[]}
121+
*/
122+
var averageOfLevels = function(root) {
123+
let res = [];
124+
let queue = [root];
125+
while (queue.length > 0) {
126+
n = queue.length;
127+
let sum = 0;
128+
for (let i = 0; i < n; i++) {
129+
let node = queue.shift();
130+
sum += node.val;
131+
if (node.left) {
132+
queue.push(node.left);
133+
}
134+
if (node.right) {
135+
queue.push(node.right);
136+
}
137+
}
138+
res.push(sum / n);
139+
}
140+
return res;
141+
};
142+
```
143+
107144
### **...**
108145

109146
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number[]}
12+
*/
13+
var averageOfLevels = function(root) {
14+
let res = [];
15+
let queue = [root];
16+
while (queue.length > 0) {
17+
n = queue.length;
18+
let sum = 0;
19+
for (let i = 0; i < n; i++) {
20+
let node = queue.shift();
21+
sum += node.val;
22+
if (node.left) {
23+
queue.push(node.left);
24+
}
25+
if (node.right) {
26+
queue.push(node.right);
27+
}
28+
}
29+
res.push(sum / n);
30+
}
31+
return res;
32+
};

0 commit comments

Comments
 (0)