@@ -212,6 +212,65 @@ func pathSum(root *TreeNode, targetSum int) [][]int {
212
212
}
213
213
```
214
214
215
+ ### ** Rust**
216
+
217
+ ``` rust
218
+ // Definition for a binary tree node.
219
+ // #[derive(Debug, PartialEq, Eq)]
220
+ // pub struct TreeNode {
221
+ // pub val: i32,
222
+ // pub left: Option<Rc<RefCell<TreeNode>>>,
223
+ // pub right: Option<Rc<RefCell<TreeNode>>>,
224
+ // }
225
+ //
226
+ // impl TreeNode {
227
+ // #[inline]
228
+ // pub fn new(val: i32) -> Self {
229
+ // TreeNode {
230
+ // val,
231
+ // left: None,
232
+ // right: None
233
+ // }
234
+ // }
235
+ // }
236
+ use std :: rc :: Rc ;
237
+ use std :: cell :: RefCell ;
238
+ impl Solution {
239
+ fn dfs (
240
+ root : Option <Rc <RefCell <TreeNode >>>,
241
+ paths : & mut Vec <i32 >,
242
+ mut target_sum : i32 ,
243
+ res : & mut Vec <Vec <i32 >>,
244
+ ) {
245
+ if let Some (node ) = root {
246
+ let mut node = node . borrow_mut ();
247
+ target_sum -= node . val;
248
+ paths . push (node . val);
249
+ if node . left. is_none () && node . right. is_none () {
250
+ if target_sum == 0 {
251
+ res . push (paths . clone ());
252
+ }
253
+ } else {
254
+ if node . left. is_some () {
255
+ Self :: dfs (node . left. take (), paths , target_sum , res );
256
+ }
257
+ if node . right. is_some () {
258
+ Self :: dfs (node . right. take (), paths , target_sum , res );
259
+ }
260
+ }
261
+ paths . pop ();
262
+ }
263
+ }
264
+
265
+ pub fn path_sum (root : Option <Rc <RefCell <TreeNode >>>, target_sum : i32 ) -> Vec <Vec <i32 >> {
266
+ let mut res = vec! [];
267
+ let mut paths = vec! [];
268
+ Self :: dfs (root , & mut paths , target_sum , & mut res );
269
+ res
270
+ }
271
+ }
272
+ ```
273
+
215
274
### ** ...**
216
275
217
276
```
0 commit comments