Skip to content

Commit 91620c4

Browse files
committed
feat: update solutions to lcof problem: No.34
面试题34. 二叉树中和为某一值的路径
1 parent 9df3b47 commit 91620c4

File tree

3 files changed

+29
-33
lines changed

3 files changed

+29
-33
lines changed

lcof/面试题34. 二叉树中和为某一值的路径/README.md

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -250,11 +250,10 @@ function pathSum(root: TreeNode | null, target: number): number[][] {
250250
if (target === 0) {
251251
res.push([...paths]);
252252
}
253-
paths.pop();
254-
return;
253+
} else {
254+
left && dfs(left, target);
255+
right && dfs(right, target);
255256
}
256-
left && dfs(left, target);
257-
right && dfs(right, target);
258257
paths.pop();
259258
};
260259
dfs(root, target);
@@ -283,9 +282,8 @@ function pathSum(root: TreeNode | null, target: number): number[][] {
283282
// }
284283
// }
285284
// }
286-
use std::cell::RefCell;
287285
use std::rc::Rc;
288-
286+
use std::cell::RefCell;
289287
impl Solution {
290288
fn dfs(
291289
root: &Option<Rc<RefCell<TreeNode>>>,
@@ -300,14 +298,15 @@ impl Solution {
300298
if node.left.is_none() && node.right.is_none() && target == 0 {
301299
res.push(paths.clone());
302300
}
303-
Solution::dfs(&node.left, target, paths, res);
304-
Solution::dfs(&node.right, target, paths, res);
301+
Self::dfs(&node.left, target, paths, res);
302+
Self::dfs(&node.right, target, paths, res);
305303
paths.pop();
306304
}
307305
}
306+
308307
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
309308
let mut res = vec![];
310-
Solution::dfs(&root, target, &mut vec![], &mut res);
309+
Self::dfs(&root, target, &mut vec![], &mut res);
311310
res
312311
}
313312
}
@@ -332,9 +331,8 @@ impl Solution {
332331
// }
333332
// }
334333
// }
335-
use std::cell::RefCell;
336334
use std::rc::Rc;
337-
335+
use std::cell::RefCell;
338336
impl Solution {
339337
fn dfs(
340338
root: &Option<Rc<RefCell<TreeNode>>>,
@@ -350,19 +348,18 @@ impl Solution {
350348
if target == 0 {
351349
res.push(paths.clone());
352350
}
353-
paths.pop();
354-
return res;
355-
}
356-
if node.left.is_some() {
357-
let res_l = Solution::dfs(&node.left, target, paths);
358-
if !res_l.is_empty() {
359-
res = [res, res_l].concat();
351+
} else {
352+
if node.left.is_some() {
353+
let res_l = Self::dfs(&node.left, target, paths);
354+
if !res_l.is_empty() {
355+
res = [res, res_l].concat();
356+
}
360357
}
361-
}
362-
if node.right.is_some() {
363-
let res_r = Solution::dfs(&node.right, target, paths);
364-
if !res_r.is_empty() {
365-
res = [res, res_r].concat();
358+
if node.right.is_some() {
359+
let res_r = Self::dfs(&node.right, target, paths);
360+
if !res_r.is_empty() {
361+
res = [res, res_r].concat();
362+
}
366363
}
367364
}
368365
paths.pop();
@@ -372,7 +369,7 @@ impl Solution {
372369
if root.is_none() {
373370
return vec![];
374371
}
375-
Solution::dfs(&root, target, &mut vec![])
372+
Self::dfs(&root, target, &mut vec![])
376373
}
377374
}
378375
```

lcof/面试题34. 二叉树中和为某一值的路径/Solution.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@
1616
// }
1717
// }
1818
// }
19-
use std::cell::RefCell;
2019
use std::rc::Rc;
21-
20+
use std::cell::RefCell;
2221
impl Solution {
2322
fn dfs(
2423
root: &Option<Rc<RefCell<TreeNode>>>,
@@ -33,14 +32,15 @@ impl Solution {
3332
if node.left.is_none() && node.right.is_none() && target == 0 {
3433
res.push(paths.clone());
3534
}
36-
Solution::dfs(&node.left, target, paths, res);
37-
Solution::dfs(&node.right, target, paths, res);
35+
Self::dfs(&node.left, target, paths, res);
36+
Self::dfs(&node.right, target, paths, res);
3837
paths.pop();
3938
}
4039
}
40+
4141
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
4242
let mut res = vec![];
43-
Solution::dfs(&root, target, &mut vec![], &mut res);
43+
Self::dfs(&root, target, &mut vec![], &mut res);
4444
res
4545
}
4646
}

lcof/面试题34. 二叉树中和为某一值的路径/Solution.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,10 @@ function pathSum(root: TreeNode | null, target: number): number[][] {
2525
if (target === 0) {
2626
res.push([...paths]);
2727
}
28-
paths.pop();
29-
return;
28+
} else {
29+
left && dfs(left, target);
30+
right && dfs(right, target);
3031
}
31-
left && dfs(left, target);
32-
right && dfs(right, target);
3332
paths.pop();
3433
};
3534
dfs(root, target);

0 commit comments

Comments
 (0)