Skip to content

Commit 69a7c9d

Browse files
committed
feat: add rust solution to lcof problem: No.34
面试题34. 二叉树中和为某一值的路径
1 parent 472200e commit 69a7c9d

File tree

2 files changed

+61
-27
lines changed

2 files changed

+61
-27
lines changed

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

+49
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,55 @@ function pathSum(root: TreeNode | null, target: number): number[][] {
280280
use std::cell::RefCell;
281281
use std::rc::Rc;
282282

283+
impl Solution {
284+
fn dfs(
285+
root: &Option<Rc<RefCell<TreeNode>>>,
286+
mut target: i32,
287+
paths: &mut Vec<i32>,
288+
res: &mut Vec<Vec<i32>>,
289+
) {
290+
if let Some(node) = root.as_ref() {
291+
let node = node.borrow();
292+
paths.push(node.val);
293+
target -= node.val;
294+
if node.left.is_none() && node.right.is_none() && target == 0 {
295+
res.push(paths.clone());
296+
}
297+
Solution::dfs(&node.left, target, paths, res);
298+
Solution::dfs(&node.right, target, paths, res);
299+
paths.pop();
300+
}
301+
}
302+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
303+
let mut res = vec![];
304+
Solution::dfs(&root, target, &mut vec![], &mut res);
305+
res
306+
}
307+
}
308+
```
309+
310+
```rust
311+
// Definition for a binary tree node.
312+
// #[derive(Debug, PartialEq, Eq)]
313+
// pub struct TreeNode {
314+
// pub val: i32,
315+
// pub left: Option<Rc<RefCell<TreeNode>>>,
316+
// pub right: Option<Rc<RefCell<TreeNode>>>,
317+
// }
318+
//
319+
// impl TreeNode {
320+
// #[inline]
321+
// pub fn new(val: i32) -> Self {
322+
// TreeNode {
323+
// val,
324+
// left: None,
325+
// right: None
326+
// }
327+
// }
328+
// }
329+
use std::cell::RefCell;
330+
use std::rc::Rc;
331+
283332
impl Solution {
284333
fn dfs(
285334
root: &Option<Rc<RefCell<TreeNode>>>,

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

+12-27
Original file line numberDiff line numberDiff line change
@@ -24,38 +24,23 @@ impl Solution {
2424
root: &Option<Rc<RefCell<TreeNode>>>,
2525
mut target: i32,
2626
paths: &mut Vec<i32>,
27-
) -> Vec<Vec<i32>> {
28-
let node = root.as_ref().unwrap().borrow();
29-
paths.push(node.val);
30-
target -= node.val;
31-
let mut res = vec![];
32-
// 确定叶结点身份
33-
if node.left.is_none() && node.right.is_none() {
34-
if target == 0 {
27+
res: &mut Vec<Vec<i32>>,
28+
) {
29+
if let Some(node) = root.as_ref() {
30+
let node = node.borrow();
31+
paths.push(node.val);
32+
target -= node.val;
33+
if node.left.is_none() && node.right.is_none() && target == 0 {
3534
res.push(paths.clone());
3635
}
36+
Solution::dfs(&node.left, target, paths, res);
37+
Solution::dfs(&node.right, target, paths, res);
3738
paths.pop();
38-
return res;
39-
}
40-
if node.left.is_some() {
41-
let res_l = Solution::dfs(&node.left, target, paths);
42-
if !res_l.is_empty() {
43-
res = [res, res_l].concat();
44-
}
45-
}
46-
if node.right.is_some() {
47-
let res_r = Solution::dfs(&node.right, target, paths);
48-
if !res_r.is_empty() {
49-
res = [res, res_r].concat();
50-
}
5139
}
52-
paths.pop();
53-
res
5440
}
5541
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
56-
if root.is_none() {
57-
return vec![];
58-
}
59-
Solution::dfs(&root, target, &mut vec![])
42+
let mut res = vec![];
43+
Solution::dfs(&root, target, &mut vec![], &mut res);
44+
res
6045
}
6146
}

0 commit comments

Comments
 (0)