Skip to content

Commit 5d84310

Browse files
committed
feat: add solution to lc problem: No.0637
No.0637.Average of Levels in Binary Tree
1 parent 0abd667 commit 5d84310

File tree

5 files changed

+354
-0
lines changed

5 files changed

+354
-0
lines changed

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

+123
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,129 @@ var averageOfLevels = function (root) {
157157
};
158158
```
159159

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+
160283
### **...**
161284

162285
```

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

+123
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,129 @@ var averageOfLevels = function (root) {
141141
};
142142
```
143143

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

146269
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
vector<double> averageOfLevels(TreeNode* root) {
15+
queue<TreeNode*> q({root});
16+
vector<double> ans;
17+
while (!q.empty()) {
18+
int n = q.size();
19+
long long sum = 0;
20+
for (int i = 0; i < n; ++i) {
21+
TreeNode* node = q.front();
22+
q.pop();
23+
sum += node->val;
24+
if (node->left != nullptr) q.push(node->left);
25+
if (node->right != nullptr) q.push(node->right);
26+
}
27+
ans.emplace_back(sum * 1.0 / n);
28+
}
29+
return ans;
30+
}
31+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func averageOfLevels(root *TreeNode) []float64 {
10+
q := []*TreeNode{root}
11+
var ans []float64
12+
for len(q) > 0 {
13+
n := len(q)
14+
var sum int
15+
for i := 0; i < n; i++ {
16+
node := q[0]
17+
q = q[1:]
18+
sum += node.Val
19+
if node.Left != nil {
20+
q = append(q, node.Left)
21+
}
22+
if node.Right != nil {
23+
q = append(q, node.Right)
24+
}
25+
}
26+
ans = append(ans, float64(sum)/float64(n))
27+
}
28+
return ans
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// Definition for a binary tree node.
2+
// #[derive(Debug, PartialEq, Eq)]
3+
// pub struct TreeNode {
4+
// pub val: i32,
5+
// pub left: Option<Rc<RefCell<TreeNode>>>,
6+
// pub right: Option<Rc<RefCell<TreeNode>>>,
7+
// }
8+
//
9+
// impl TreeNode {
10+
// #[inline]
11+
// pub fn new(val: i32) -> Self {
12+
// TreeNode {
13+
// val,
14+
// left: None,
15+
// right: None
16+
// }
17+
// }
18+
// }
19+
use std::rc::Rc;
20+
use std::cell::RefCell;
21+
use std::collections::VecDeque;
22+
impl Solution {
23+
pub fn average_of_levels(root: Option<Rc<RefCell<TreeNode>>>) -> Vec<f64> {
24+
if root.is_none() {
25+
return Vec::new();
26+
}
27+
28+
let mut q = VecDeque::new();
29+
q.push_back(Rc::clone(&root.unwrap()));
30+
let mut ans = Vec::new();
31+
while !q.is_empty() {
32+
let n = q.len();
33+
let mut sum = 0.0;
34+
for _ in 0..n {
35+
let node = q.pop_front().unwrap();
36+
sum += node.borrow().val as f64;
37+
if node.borrow().left.is_some() {
38+
q.push_back(Rc::clone(node.borrow().left.as_ref().unwrap()));
39+
}
40+
if node.borrow().right.is_some() {
41+
q.push_back(Rc::clone(node.borrow().right.as_ref().unwrap()));
42+
}
43+
}
44+
ans.push(sum / n as f64);
45+
}
46+
ans
47+
}
48+
}

0 commit comments

Comments
 (0)