Skip to content

Commit 4aed2c8

Browse files
committed
fix: synchronous solutions to lcof problem: No.34
面试题34. 二叉树中和为某一值的路径
1 parent f7df505 commit 4aed2c8

File tree

2 files changed

+25
-20
lines changed

2 files changed

+25
-20
lines changed

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

+18-17
Original file line numberDiff line numberDiff line change
@@ -224,27 +224,26 @@ public:
224224
*/
225225

226226
function pathSum(root: TreeNode | null, target: number): number[][] {
227-
const res = [];
227+
const res: number[][] = [];
228228
if (root == null) {
229229
return res;
230230
}
231-
const dfs = (
232-
{ val, right, left }: TreeNode,
233-
target: number,
234-
values: number[]
235-
) => {
236-
values.push(val);
231+
const paths: number[] = [];
232+
const dfs = ({ val, right, left }: TreeNode, target: number) => {
233+
paths.push(val);
237234
target -= val;
238235
if (left == null && right == null) {
239236
if (target === 0) {
240-
res.push(values);
237+
res.push([...paths]);
241238
}
239+
paths.pop();
242240
return;
243241
}
244-
left && dfs(left, target, [...values]);
245-
right && dfs(right, target, [...values]);
242+
left && dfs(left, target);
243+
right && dfs(right, target);
244+
paths.pop();
246245
};
247-
dfs(root, target, []);
246+
dfs(root, target);
248247
return res;
249248
}
250249
```
@@ -277,38 +276,40 @@ impl Solution {
277276
fn dfs(
278277
root: &Option<Rc<RefCell<TreeNode>>>,
279278
mut target: i32,
280-
mut values: Vec<i32>,
279+
paths: &mut Vec<i32>,
281280
) -> Vec<Vec<i32>> {
282281
let node = root.as_ref().unwrap().borrow();
283-
values.push(node.val);
282+
paths.push(node.val);
284283
target -= node.val;
285284
let mut res = vec![];
286285
// 确定叶结点身份
287286
if node.left.is_none() && node.right.is_none() {
288287
if target == 0 {
289-
res.push(values);
288+
res.push(paths.clone());
290289
}
290+
paths.pop();
291291
return res;
292292
}
293293
if node.left.is_some() {
294-
let res_l = Solution::dfs(&node.left, target, values.clone());
294+
let res_l = Solution::dfs(&node.left, target, paths);
295295
if !res_l.is_empty() {
296296
res = [res, res_l].concat();
297297
}
298298
}
299299
if node.right.is_some() {
300-
let res_r = Solution::dfs(&node.right, target, values.clone());
300+
let res_r = Solution::dfs(&node.right, target, paths);
301301
if !res_r.is_empty() {
302302
res = [res, res_r].concat();
303303
}
304304
}
305+
paths.pop();
305306
res
306307
}
307308
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
308309
if root.is_none() {
309310
return vec![];
310311
}
311-
Solution::dfs(&root, target, vec![])
312+
Solution::dfs(&root, target, &mut vec![])
312313
}
313314
}
314315
```

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,15 @@
1616
// }
1717
// }
1818
// }
19-
use std::rc::Rc;
2019
use std::cell::RefCell;
20+
use std::rc::Rc;
2121

2222
impl Solution {
23-
fn dfs(root: &Option<Rc<RefCell<TreeNode>>>, mut target: i32, paths: &mut Vec<i32>) -> Vec<Vec<i32>> {
23+
fn dfs(
24+
root: &Option<Rc<RefCell<TreeNode>>>,
25+
mut target: i32,
26+
paths: &mut Vec<i32>,
27+
) -> Vec<Vec<i32>> {
2428
let node = root.as_ref().unwrap().borrow();
2529
paths.push(node.val);
2630
target -= node.val;
@@ -54,4 +58,4 @@ impl Solution {
5458
}
5559
Solution::dfs(&root, target, &mut vec![])
5660
}
57-
}
61+
}

0 commit comments

Comments
 (0)