@@ -157,6 +157,129 @@ var averageOfLevels = function (root) {
157
157
};
158
158
```
159
159
160
+ ### ** Go**
161
+
162
+ ``` go
163
+ /* *
164
+ * Definition for a binary tree node.
165
+ * type TreeNode struct {
166
+ * Val int
167
+ * Left *TreeNode
168
+ * Right *TreeNode
169
+ * }
170
+ */
171
+ func averageOfLevels (root *TreeNode ) []float64 {
172
+ q := []*TreeNode{root}
173
+ var ans []float64
174
+ for len (q) > 0 {
175
+ n := len (q)
176
+ var sum int
177
+ for i := 0 ; i < n; i++ {
178
+ node := q[0 ]
179
+ q = q[1 :]
180
+ sum += node.Val
181
+ if node.Left != nil {
182
+ q = append (q, node.Left )
183
+ }
184
+ if node.Right != nil {
185
+ q = append (q, node.Right )
186
+ }
187
+ }
188
+ ans = append (ans, float64 (sum)/float64 (n))
189
+ }
190
+ return ans
191
+ }
192
+ ```
193
+
194
+ ### ** C++**
195
+
196
+ ``` cpp
197
+ /* *
198
+ * Definition for a binary tree node.
199
+ * struct TreeNode {
200
+ * int val;
201
+ * TreeNode *left;
202
+ * TreeNode *right;
203
+ * TreeNode() : val(0), left(nullptr), right(nullptr) {}
204
+ * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
205
+ * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
206
+ * };
207
+ */
208
+ class Solution {
209
+ public:
210
+ vector<double > averageOfLevels(TreeNode* root) {
211
+ queue<TreeNode* > q({root});
212
+ vector<double > ans;
213
+ while (!q.empty()) {
214
+ int n = q.size();
215
+ long long sum = 0;
216
+ for (int i = 0; i < n; ++i) {
217
+ TreeNode* node = q.front();
218
+ q.pop();
219
+ sum += node->val;
220
+ if (node->left != nullptr) q.push(node->left);
221
+ if (node->right != nullptr) q.push(node->right);
222
+ }
223
+ ans.emplace_back(sum * 1.0 / n);
224
+ }
225
+ return ans;
226
+ }
227
+ };
228
+ ```
229
+
230
+ ### **Rust**
231
+
232
+ ```rust
233
+ // Definition for a binary tree node.
234
+ // #[derive(Debug, PartialEq, Eq)]
235
+ // pub struct TreeNode {
236
+ // pub val: i32,
237
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
238
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
239
+ // }
240
+ //
241
+ // impl TreeNode {
242
+ // #[inline]
243
+ // pub fn new(val: i32) -> Self {
244
+ // TreeNode {
245
+ // val,
246
+ // left: None,
247
+ // right: None
248
+ // }
249
+ // }
250
+ // }
251
+ use std::rc::Rc;
252
+ use std::cell::RefCell;
253
+ use std::collections::VecDeque;
254
+ impl Solution {
255
+ pub fn average_of_levels(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<f64> {
256
+ if root.is_none() {
257
+ return Vec::new();
258
+ }
259
+
260
+ let mut q = VecDeque::new();
261
+ q.push_back(Rc::clone(&root.unwrap()));
262
+ let mut ans = Vec::new();
263
+ while !q.is_empty() {
264
+ let n = q.len();
265
+ let mut sum = 0.0;
266
+ for _ in 0..n {
267
+ let node = q.pop_front().unwrap();
268
+ sum += node.borrow().val as f64;
269
+ if node.borrow().left.is_some() {
270
+ q.push_back(Rc::clone(node.borrow().left.as_ref().unwrap()));
271
+ }
272
+ if node.borrow().right.is_some() {
273
+ q.push_back(Rc::clone(node.borrow().right.as_ref().unwrap()));
274
+ }
275
+ }
276
+ ans.push(sum / n as f64);
277
+ }
278
+ ans
279
+ }
280
+ }
281
+ ```
282
+
160
283
### ** ...**
161
284
162
285
```
0 commit comments