Skip to content

Commit b8095a8

Browse files
committed
feat: add Rust solution for lc No.1161
1 parent 2f934c0 commit b8095a8

File tree

5 files changed

+272
-61
lines changed

5 files changed

+272
-61
lines changed

solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/README.md

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,17 @@ public:
177177
root = q.front();
178178
q.pop();
179179
s += root->val;
180-
if (root->left) q.push(root->left);
181-
if (root->right) q.push(root->right);
180+
if (root->left) {
181+
q.push(root->left);
182+
}
183+
if (root->right) {
184+
q.push(root->right);
185+
}
186+
}
187+
if (mx < s) {
188+
mx = s;
189+
ans = i;
182190
}
183-
if (mx < s) mx = s, ans = i;
184191
}
185192
return ans;
186193
}
@@ -244,26 +251,91 @@ func maxLevelSum(root *TreeNode) int {
244251
*/
245252

246253
function maxLevelSum(root: TreeNode | null): number {
247-
const queue = [root];
248-
let res = 1;
249-
let max = -Infinity;
250-
let h = 1;
251-
while (queue.length !== 0) {
252-
const n = queue.length;
253-
let sum = 0;
254-
for (let i = 0; i < n; i++) {
255-
const { val, left, right } = queue.shift();
256-
sum += val;
257-
left && queue.push(left);
258-
right && queue.push(right);
254+
let q = [root];
255+
let i = 0;
256+
let mx = -Infinity;
257+
let ans = 0;
258+
while (q.length) {
259+
++i;
260+
const nq = [];
261+
let s = 0;
262+
for (const { val, left, right } of q) {
263+
s += val;
264+
left && nq.push(left);
265+
right && nq.push(right);
266+
}
267+
if (mx < s) {
268+
mx = s;
269+
ans = i;
259270
}
260-
if (sum > max) {
261-
max = sum;
262-
res = h;
271+
q = nq;
272+
}
273+
return ans;
274+
}
275+
```
276+
277+
#### Rust
278+
279+
```rust
280+
// Definition for a binary tree node.
281+
// #[derive(Debug, PartialEq, Eq)]
282+
// pub struct TreeNode {
283+
// pub val: i32,
284+
// pub left: Option<Rc<RefCell<TreeNode>>>,
285+
// pub right: Option<Rc<RefCell<TreeNode>>>,
286+
// }
287+
//
288+
// impl TreeNode {
289+
// #[inline]
290+
// pub fn new(val: i32) -> Self {
291+
// TreeNode {
292+
// val,
293+
// left: None,
294+
// right: None
295+
// }
296+
// }
297+
// }
298+
use std::cell::RefCell;
299+
use std::rc::Rc;
300+
use std::collections::VecDeque;
301+
impl Solution {
302+
pub fn max_level_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
303+
let mut q = VecDeque::new();
304+
if let Some(r) = root {
305+
q.push_back(r);
306+
}
307+
308+
let mut i = 0;
309+
let mut mx = i32::MIN;
310+
let mut ans = 0;
311+
312+
while !q.is_empty() {
313+
i += 1;
314+
let mut s = 0;
315+
let sz = q.len();
316+
317+
for _ in 0..sz {
318+
let node = q.pop_front().unwrap();
319+
let node = node.borrow();
320+
321+
s += node.val;
322+
323+
if let Some(left) = node.left.clone() {
324+
q.push_back(left);
325+
}
326+
if let Some(right) = node.right.clone() {
327+
q.push_back(right);
328+
}
329+
}
330+
331+
if s > mx {
332+
mx = s;
333+
ans = i;
334+
}
263335
}
264-
h++;
336+
337+
ans
265338
}
266-
return res;
267339
}
268340
```
269341

solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/README_EN.md

Lines changed: 92 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,17 @@ public:
173173
root = q.front();
174174
q.pop();
175175
s += root->val;
176-
if (root->left) q.push(root->left);
177-
if (root->right) q.push(root->right);
176+
if (root->left) {
177+
q.push(root->left);
178+
}
179+
if (root->right) {
180+
q.push(root->right);
181+
}
182+
}
183+
if (mx < s) {
184+
mx = s;
185+
ans = i;
178186
}
179-
if (mx < s) mx = s, ans = i;
180187
}
181188
return ans;
182189
}
@@ -240,26 +247,91 @@ func maxLevelSum(root *TreeNode) int {
240247
*/
241248

242249
function maxLevelSum(root: TreeNode | null): number {
243-
const queue = [root];
244-
let res = 1;
245-
let max = -Infinity;
246-
let h = 1;
247-
while (queue.length !== 0) {
248-
const n = queue.length;
249-
let sum = 0;
250-
for (let i = 0; i < n; i++) {
251-
const { val, left, right } = queue.shift();
252-
sum += val;
253-
left && queue.push(left);
254-
right && queue.push(right);
250+
let q = [root];
251+
let i = 0;
252+
let mx = -Infinity;
253+
let ans = 0;
254+
while (q.length) {
255+
++i;
256+
const nq = [];
257+
let s = 0;
258+
for (const { val, left, right } of q) {
259+
s += val;
260+
left && nq.push(left);
261+
right && nq.push(right);
262+
}
263+
if (mx < s) {
264+
mx = s;
265+
ans = i;
255266
}
256-
if (sum > max) {
257-
max = sum;
258-
res = h;
267+
q = nq;
268+
}
269+
return ans;
270+
}
271+
```
272+
273+
#### Rust
274+
275+
```rust
276+
// Definition for a binary tree node.
277+
// #[derive(Debug, PartialEq, Eq)]
278+
// pub struct TreeNode {
279+
// pub val: i32,
280+
// pub left: Option<Rc<RefCell<TreeNode>>>,
281+
// pub right: Option<Rc<RefCell<TreeNode>>>,
282+
// }
283+
//
284+
// impl TreeNode {
285+
// #[inline]
286+
// pub fn new(val: i32) -> Self {
287+
// TreeNode {
288+
// val,
289+
// left: None,
290+
// right: None
291+
// }
292+
// }
293+
// }
294+
use std::cell::RefCell;
295+
use std::rc::Rc;
296+
use std::collections::VecDeque;
297+
impl Solution {
298+
pub fn max_level_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
299+
let mut q = VecDeque::new();
300+
if let Some(r) = root {
301+
q.push_back(r);
302+
}
303+
304+
let mut i = 0;
305+
let mut mx = i32::MIN;
306+
let mut ans = 0;
307+
308+
while !q.is_empty() {
309+
i += 1;
310+
let mut s = 0;
311+
let sz = q.len();
312+
313+
for _ in 0..sz {
314+
let node = q.pop_front().unwrap();
315+
let node = node.borrow();
316+
317+
s += node.val;
318+
319+
if let Some(left) = node.left.clone() {
320+
q.push_back(left);
321+
}
322+
if let Some(right) = node.right.clone() {
323+
q.push_back(right);
324+
}
325+
}
326+
327+
if s > mx {
328+
mx = s;
329+
ans = i;
330+
}
259331
}
260-
h++;
332+
333+
ans
261334
}
262-
return res;
263335
}
264336
```
265337

solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/Solution.cpp

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,18 @@ class Solution {
2323
root = q.front();
2424
q.pop();
2525
s += root->val;
26-
if (root->left) q.push(root->left);
27-
if (root->right) q.push(root->right);
26+
if (root->left) {
27+
q.push(root->left);
28+
}
29+
if (root->right) {
30+
q.push(root->right);
31+
}
32+
}
33+
if (mx < s) {
34+
mx = s;
35+
ans = i;
2836
}
29-
if (mx < s) mx = s, ans = i;
3037
}
3138
return ans;
3239
}
33-
};
40+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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::cell::RefCell;
20+
use std::rc::Rc;
21+
use std::collections::VecDeque;
22+
impl Solution {
23+
pub fn max_level_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
24+
let mut q = VecDeque::new();
25+
if let Some(r) = root {
26+
q.push_back(r);
27+
}
28+
29+
let mut i = 0;
30+
let mut mx = i32::MIN;
31+
let mut ans = 0;
32+
33+
while !q.is_empty() {
34+
i += 1;
35+
let mut s = 0;
36+
let sz = q.len();
37+
38+
for _ in 0..sz {
39+
let node = q.pop_front().unwrap();
40+
let node = node.borrow();
41+
42+
s += node.val;
43+
44+
if let Some(left) = node.left.clone() {
45+
q.push_back(left);
46+
}
47+
if let Some(right) = node.right.clone() {
48+
q.push_back(right);
49+
}
50+
}
51+
52+
if s > mx {
53+
mx = s;
54+
ans = i;
55+
}
56+
}
57+
58+
ans
59+
}
60+
}

solution/1100-1199/1161.Maximum Level Sum of a Binary Tree/Solution.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@
1313
*/
1414

1515
function maxLevelSum(root: TreeNode | null): number {
16-
const queue = [root];
17-
let res = 1;
18-
let max = -Infinity;
19-
let h = 1;
20-
while (queue.length !== 0) {
21-
const n = queue.length;
22-
let sum = 0;
23-
for (let i = 0; i < n; i++) {
24-
const { val, left, right } = queue.shift();
25-
sum += val;
26-
left && queue.push(left);
27-
right && queue.push(right);
16+
let q = [root];
17+
let i = 0;
18+
let mx = -Infinity;
19+
let ans = 0;
20+
while (q.length) {
21+
++i;
22+
const nq = [];
23+
let s = 0;
24+
for (const { val, left, right } of q) {
25+
s += val;
26+
left && nq.push(left);
27+
right && nq.push(right);
2828
}
29-
if (sum > max) {
30-
max = sum;
31-
res = h;
29+
if (mx < s) {
30+
mx = s;
31+
ans = i;
3232
}
33-
h++;
33+
q = nq;
3434
}
35-
return res;
35+
return ans;
3636
}

0 commit comments

Comments
 (0)