@@ -250,11 +250,10 @@ function pathSum(root: TreeNode | null, target: number): number[][] {
250
250
if (target === 0 ) {
251
251
res .push ([... paths ]);
252
252
}
253
- paths .pop ();
254
- return ;
253
+ } else {
254
+ left && dfs (left , target );
255
+ right && dfs (right , target );
255
256
}
256
- left && dfs (left , target );
257
- right && dfs (right , target );
258
257
paths .pop ();
259
258
};
260
259
dfs (root , target );
@@ -283,9 +282,8 @@ function pathSum(root: TreeNode | null, target: number): number[][] {
283
282
// }
284
283
// }
285
284
// }
286
- use std :: cell :: RefCell ;
287
285
use std :: rc :: Rc ;
288
-
286
+ use std :: cell :: RefCell ;
289
287
impl Solution {
290
288
fn dfs (
291
289
root : & Option <Rc <RefCell <TreeNode >>>,
@@ -300,14 +298,15 @@ impl Solution {
300
298
if node . left. is_none () && node . right. is_none () && target == 0 {
301
299
res . push (paths . clone ());
302
300
}
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 );
305
303
paths . pop ();
306
304
}
307
305
}
306
+
308
307
pub fn path_sum (root : Option <Rc <RefCell <TreeNode >>>, target : i32 ) -> Vec <Vec <i32 >> {
309
308
let mut res = vec! [];
310
- Solution :: dfs (& root , target , & mut vec! [], & mut res );
309
+ Self :: dfs (& root , target , & mut vec! [], & mut res );
311
310
res
312
311
}
313
312
}
@@ -332,9 +331,8 @@ impl Solution {
332
331
// }
333
332
// }
334
333
// }
335
- use std :: cell :: RefCell ;
336
334
use std :: rc :: Rc ;
337
-
335
+ use std :: cell :: RefCell ;
338
336
impl Solution {
339
337
fn dfs (
340
338
root : & Option <Rc <RefCell <TreeNode >>>,
@@ -350,19 +348,18 @@ impl Solution {
350
348
if target == 0 {
351
349
res . push (paths . clone ());
352
350
}
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
+ }
360
357
}
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
+ }
366
363
}
367
364
}
368
365
paths . pop ();
@@ -372,7 +369,7 @@ impl Solution {
372
369
if root . is_none () {
373
370
return vec! [];
374
371
}
375
- Solution :: dfs (& root , target , & mut vec! [])
372
+ Self :: dfs (& root , target , & mut vec! [])
376
373
}
377
374
}
378
375
```
0 commit comments