Skip to content

Commit b748286

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

File tree

3 files changed

+204
-0
lines changed

3 files changed

+204
-0
lines changed

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

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,113 @@ public:
206206
};
207207
```
208208

209+
### **TypeScript**
210+
211+
```ts
212+
/**
213+
* Definition for a binary tree node.
214+
* class TreeNode {
215+
* val: number
216+
* left: TreeNode | null
217+
* right: TreeNode | null
218+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
219+
* this.val = (val===undefined ? 0 : val)
220+
* this.left = (left===undefined ? null : left)
221+
* this.right = (right===undefined ? null : right)
222+
* }
223+
* }
224+
*/
225+
226+
function pathSum(root: TreeNode | null, target: number): number[][] {
227+
const res = [];
228+
if (root == null) {
229+
return res;
230+
}
231+
const dfs = (
232+
{ val, right, left }: TreeNode,
233+
target: number,
234+
values: number[]
235+
) => {
236+
values.push(val);
237+
target -= val;
238+
if (left == null && right == null) {
239+
if (target === 0) {
240+
res.push(values);
241+
}
242+
return;
243+
}
244+
left && dfs(left, target, [...values]);
245+
right && dfs(right, target, [...values]);
246+
};
247+
dfs(root, target, []);
248+
return res;
249+
}
250+
```
251+
252+
### **Rust**
253+
254+
```rust
255+
// Definition for a binary tree node.
256+
// #[derive(Debug, PartialEq, Eq)]
257+
// pub struct TreeNode {
258+
// pub val: i32,
259+
// pub left: Option<Rc<RefCell<TreeNode>>>,
260+
// pub right: Option<Rc<RefCell<TreeNode>>>,
261+
// }
262+
//
263+
// impl TreeNode {
264+
// #[inline]
265+
// pub fn new(val: i32) -> Self {
266+
// TreeNode {
267+
// val,
268+
// left: None,
269+
// right: None
270+
// }
271+
// }
272+
// }
273+
use std::cell::RefCell;
274+
use std::rc::Rc;
275+
276+
impl Solution {
277+
fn dfs(
278+
root: &Option<Rc<RefCell<TreeNode>>>,
279+
mut target: i32,
280+
mut values: Vec<i32>,
281+
) -> Vec<Vec<i32>> {
282+
let node = root.as_ref().unwrap().borrow();
283+
values.push(node.val);
284+
target -= node.val;
285+
let mut res = vec![];
286+
// 确定叶结点身份
287+
if node.left.is_none() && node.right.is_none() {
288+
if target == 0 {
289+
res.push(values);
290+
}
291+
return res;
292+
}
293+
if node.left.is_some() {
294+
let res_l = Solution::dfs(&node.left, target, values.clone());
295+
if !res_l.is_empty() {
296+
res = [res, res_l].concat();
297+
}
298+
}
299+
if node.right.is_some() {
300+
let res_r = Solution::dfs(&node.right, target, values.clone());
301+
if !res_r.is_empty() {
302+
res = [res, res_r].concat();
303+
}
304+
}
305+
res
306+
}
307+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
308+
if root.is_none() {
309+
return vec![];
310+
}
311+
Solution::dfs(&root, target, vec![])
312+
}
313+
}
314+
```
315+
209316
### **...**
210317

211318
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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+
22+
impl Solution {
23+
fn dfs(
24+
root: &Option<Rc<RefCell<TreeNode>>>,
25+
mut target: i32,
26+
mut values: Vec<i32>,
27+
) -> Vec<Vec<i32>> {
28+
let node = root.as_ref().unwrap().borrow();
29+
values.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 {
35+
res.push(values);
36+
}
37+
return res;
38+
}
39+
if node.left.is_some() {
40+
let res_l = Solution::dfs(&node.left, target, values.clone());
41+
if !res_l.is_empty() {
42+
res = [res, res_l].concat();
43+
}
44+
}
45+
if node.right.is_some() {
46+
let res_r = Solution::dfs(&node.right, target, values.clone());
47+
if !res_r.is_empty() {
48+
res = [res, res_r].concat();
49+
}
50+
}
51+
res
52+
}
53+
pub fn path_sum(root: Option<Rc<RefCell<TreeNode>>>, target: i32) -> Vec<Vec<i32>> {
54+
if root.is_none() {
55+
return vec![];
56+
}
57+
Solution::dfs(&root, target, vec![])
58+
}
59+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function pathSum(root: TreeNode | null, target: number): number[][] {
16+
const res = [];
17+
if (root == null) {
18+
return res;
19+
}
20+
const dfs = (
21+
{ val, right, left }: TreeNode,
22+
target: number,
23+
values: number[]
24+
) => {
25+
values.push(val);
26+
target -= val;
27+
if (left == null && right == null) {
28+
if (target === 0) {
29+
res.push(values);
30+
}
31+
return;
32+
}
33+
left && dfs(left, target, [...values]);
34+
right && dfs(right, target, [...values]);
35+
};
36+
dfs(root, target, []);
37+
return res;
38+
}

0 commit comments

Comments
 (0)