Skip to content

Commit 69b99f2

Browse files
committed
feat: add solutions to lc problem: No.1302
No.1302.Deepest Leaves Sum
1 parent d0c5924 commit 69b99f2

File tree

5 files changed

+434
-1
lines changed

5 files changed

+434
-1
lines changed

solution/1300-1399/1302.Deepest Leaves Sum/README.md

+165-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@
3939

4040
<!-- 这里可写通用的实现逻辑 -->
4141

42-
广度优先搜索。
42+
**方法一:BFS**
43+
44+
可以忽略一些细节,每次都统计当前遍历层级的数值和,当 BFS 结束时,最后一次数值和便是结果。
45+
46+
**方法二:DFS**
4347

4448
<!-- tabs:start -->
4549

@@ -185,6 +189,166 @@ func deepestLeavesSum(root *TreeNode) int {
185189
}
186190
```
187191

192+
### **C**
193+
194+
```c
195+
/**
196+
* Definition for a binary tree node.
197+
* struct TreeNode {
198+
* int val;
199+
* struct TreeNode *left;
200+
* struct TreeNode *right;
201+
* };
202+
*/
203+
204+
205+
void dfs(struct TreeNode* root, int depth, int* maxDepth, int* res) {
206+
if (!root->left && !root->right) {
207+
if (depth == *maxDepth) {
208+
*res += root->val;
209+
} else if (depth > *maxDepth) {
210+
*maxDepth = depth;
211+
*res = root->val;
212+
}
213+
return;
214+
}
215+
if (root->left) {
216+
dfs(root->left, depth + 1, maxDepth, res);
217+
}
218+
if (root->right) {
219+
dfs(root->right, depth + 1, maxDepth, res);
220+
}
221+
}
222+
223+
int deepestLeavesSum(struct TreeNode* root){
224+
int res = 0;
225+
int maxDepth = 0;
226+
dfs(root, 0, &maxDepth, &res);
227+
return res;
228+
}
229+
```
230+
231+
### **TypeScript**
232+
233+
```ts
234+
/**
235+
* Definition for a binary tree node.
236+
* class TreeNode {
237+
* val: number
238+
* left: TreeNode | null
239+
* right: TreeNode | null
240+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
241+
* this.val = (val===undefined ? 0 : val)
242+
* this.left = (left===undefined ? null : left)
243+
* this.right = (right===undefined ? null : right)
244+
* }
245+
* }
246+
*/
247+
248+
function deepestLeavesSum(root: TreeNode | null): number {
249+
const queue = [root];
250+
let res = 0;
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);
259+
}
260+
res = sum;
261+
}
262+
return res;
263+
}
264+
```
265+
266+
```ts
267+
/**
268+
* Definition for a binary tree node.
269+
* class TreeNode {
270+
* val: number
271+
* left: TreeNode | null
272+
* right: TreeNode | null
273+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
274+
* this.val = (val===undefined ? 0 : val)
275+
* this.left = (left===undefined ? null : left)
276+
* this.right = (right===undefined ? null : right)
277+
* }
278+
* }
279+
*/
280+
281+
function deepestLeavesSum(root: TreeNode | null): number {
282+
let res = 0;
283+
let maxDepath = 0;
284+
const dfs = ({ val, left, right }: TreeNode, depth: number) => {
285+
if (left == null && right == null) {
286+
if (depth === maxDepath) {
287+
res += val;
288+
} else if (depth > maxDepath) {
289+
maxDepath = depth;
290+
res = val;
291+
}
292+
return;
293+
}
294+
left && dfs(left, depth + 1);
295+
right && dfs(right, depth + 1);
296+
};
297+
dfs(root, 0);
298+
return res;
299+
}
300+
```
301+
302+
### **Rust**
303+
304+
```rust
305+
// Definition for a binary tree node.
306+
// #[derive(Debug, PartialEq, Eq)]
307+
// pub struct TreeNode {
308+
// pub val: i32,
309+
// pub left: Option<Rc<RefCell<TreeNode>>>,
310+
// pub right: Option<Rc<RefCell<TreeNode>>>,
311+
// }
312+
//
313+
// impl TreeNode {
314+
// #[inline]
315+
// pub fn new(val: i32) -> Self {
316+
// TreeNode {
317+
// val,
318+
// left: None,
319+
// right: None
320+
// }
321+
// }
322+
// }
323+
use std::rc::Rc;
324+
use std::cell::RefCell;
325+
impl Solution {
326+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, depth: i32, max_depth: &mut i32, res: &mut i32) {
327+
if let Some(node) = root {
328+
let node = node.borrow();
329+
if node.left.is_none() && node.right.is_none() {
330+
if depth == *max_depth {
331+
*res += node.val;
332+
} else if depth > *max_depth {
333+
*max_depth = depth;
334+
*res = node.val
335+
}
336+
return;
337+
}
338+
Self::dfs(&node.left, depth + 1, max_depth, res);
339+
Self::dfs(&node.right, depth + 1, max_depth, res);
340+
}
341+
}
342+
343+
pub fn deepest_leaves_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
344+
let mut res = 0;
345+
let mut max_depth = 0;
346+
Self::dfs(&root, 0, &mut max_depth, &mut res);
347+
res
348+
}
349+
}
350+
```
351+
188352
### **...**
189353

190354
```

solution/1300-1399/1302.Deepest Leaves Sum/README_EN.md

+160
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,166 @@ func deepestLeavesSum(root *TreeNode) int {
173173
}
174174
```
175175

176+
### **C**
177+
178+
```c
179+
/**
180+
* Definition for a binary tree node.
181+
* struct TreeNode {
182+
* int val;
183+
* struct TreeNode *left;
184+
* struct TreeNode *right;
185+
* };
186+
*/
187+
188+
189+
void dfs(struct TreeNode* root, int depth, int* maxDepth, int* res) {
190+
if (!root->left && !root->right) {
191+
if (depth == *maxDepth) {
192+
*res += root->val;
193+
} else if (depth > *maxDepth) {
194+
*maxDepth = depth;
195+
*res = root->val;
196+
}
197+
return;
198+
}
199+
if (root->left) {
200+
dfs(root->left, depth + 1, maxDepth, res);
201+
}
202+
if (root->right) {
203+
dfs(root->right, depth + 1, maxDepth, res);
204+
}
205+
}
206+
207+
int deepestLeavesSum(struct TreeNode* root){
208+
int res = 0;
209+
int maxDepth = 0;
210+
dfs(root, 0, &maxDepth, &res);
211+
return res;
212+
}
213+
```
214+
215+
### **TypeScript**
216+
217+
```ts
218+
/**
219+
* Definition for a binary tree node.
220+
* class TreeNode {
221+
* val: number
222+
* left: TreeNode | null
223+
* right: TreeNode | null
224+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
225+
* this.val = (val===undefined ? 0 : val)
226+
* this.left = (left===undefined ? null : left)
227+
* this.right = (right===undefined ? null : right)
228+
* }
229+
* }
230+
*/
231+
232+
function deepestLeavesSum(root: TreeNode | null): number {
233+
const queue = [root];
234+
let res = 0;
235+
while (queue.length !== 0) {
236+
const n = queue.length;
237+
let sum = 0;
238+
for (let i = 0; i < n; i++) {
239+
const { val, left, right } = queue.shift();
240+
sum += val;
241+
left && queue.push(left);
242+
right && queue.push(right);
243+
}
244+
res = sum;
245+
}
246+
return res;
247+
}
248+
```
249+
250+
```ts
251+
/**
252+
* Definition for a binary tree node.
253+
* class TreeNode {
254+
* val: number
255+
* left: TreeNode | null
256+
* right: TreeNode | null
257+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
258+
* this.val = (val===undefined ? 0 : val)
259+
* this.left = (left===undefined ? null : left)
260+
* this.right = (right===undefined ? null : right)
261+
* }
262+
* }
263+
*/
264+
265+
function deepestLeavesSum(root: TreeNode | null): number {
266+
let res = 0;
267+
let maxDepath = 0;
268+
const dfs = ({ val, left, right }: TreeNode, depth: number) => {
269+
if (left == null && right == null) {
270+
if (depth === maxDepath) {
271+
res += val;
272+
} else if (depth > maxDepath) {
273+
maxDepath = depth;
274+
res = val;
275+
}
276+
return;
277+
}
278+
left && dfs(left, depth + 1);
279+
right && dfs(right, depth + 1);
280+
};
281+
dfs(root, 0);
282+
return res;
283+
}
284+
```
285+
286+
### **Rust**
287+
288+
```rust
289+
// Definition for a binary tree node.
290+
// #[derive(Debug, PartialEq, Eq)]
291+
// pub struct TreeNode {
292+
// pub val: i32,
293+
// pub left: Option<Rc<RefCell<TreeNode>>>,
294+
// pub right: Option<Rc<RefCell<TreeNode>>>,
295+
// }
296+
//
297+
// impl TreeNode {
298+
// #[inline]
299+
// pub fn new(val: i32) -> Self {
300+
// TreeNode {
301+
// val,
302+
// left: None,
303+
// right: None
304+
// }
305+
// }
306+
// }
307+
use std::rc::Rc;
308+
use std::cell::RefCell;
309+
impl Solution {
310+
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, depth: i32, max_depth: &mut i32, res: &mut i32) {
311+
if let Some(node) = root {
312+
let node = node.borrow();
313+
if node.left.is_none() && node.right.is_none() {
314+
if depth == *max_depth {
315+
*res += node.val;
316+
} else if depth > *max_depth {
317+
*max_depth = depth;
318+
*res = node.val
319+
}
320+
return;
321+
}
322+
Self::dfs(&node.left, depth + 1, max_depth, res);
323+
Self::dfs(&node.right, depth + 1, max_depth, res);
324+
}
325+
}
326+
327+
pub fn deepest_leaves_sum(root: Option<Rc<RefCell<TreeNode>>>) -> i32 {
328+
let mut res = 0;
329+
let mut max_depth = 0;
330+
Self::dfs(&root, 0, &mut max_depth, &mut res);
331+
res
332+
}
333+
}
334+
```
335+
176336
### **...**
177337

178338
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* struct TreeNode *left;
6+
* struct TreeNode *right;
7+
* };
8+
*/
9+
10+
11+
void dfs(struct TreeNode* root, int depth, int* maxDepth, int* res) {
12+
if (!root->left && !root->right) {
13+
if (depth == *maxDepth) {
14+
*res += root->val;
15+
} else if (depth > *maxDepth) {
16+
*maxDepth = depth;
17+
*res = root->val;
18+
}
19+
return;
20+
}
21+
if (root->left) {
22+
dfs(root->left, depth + 1, maxDepth, res);
23+
}
24+
if (root->right) {
25+
dfs(root->right, depth + 1, maxDepth, res);
26+
}
27+
}
28+
29+
int deepestLeavesSum(struct TreeNode* root){
30+
int res = 0;
31+
int maxDepth = 0;
32+
dfs(root, 0, &maxDepth, &res);
33+
return res;
34+
}

0 commit comments

Comments
 (0)