@@ -228,26 +228,26 @@ func pathSum(root *TreeNode, target int) (ans [][]int) {
228
228
*/
229
229
230
230
function pathSum(root : TreeNode | null , target : number ): number [][] {
231
- const res: number [][] = [];
232
- if (root == null ) {
233
- return res ;
234
- }
235
- const paths: number [] = [];
236
- const dfs = ({ val , right , left }: TreeNode , target : number ) => {
237
- paths .push (val );
238
- target -= val ;
239
- if (left == null && right == null ) {
240
- if (target === 0 ) {
241
- res .push ([... paths ]);
242
- }
243
- } else {
244
- left && dfs (left , target );
245
- right && dfs (right , target );
231
+ const ans: number [][] = [];
232
+ const t: number [] = [];
233
+
234
+ const dfs = (root : TreeNode | null , s : number ): void => {
235
+ if (! root ) {
236
+ return ;
237
+ }
238
+ const { val, left, right } = root ;
239
+ t .push (val );
240
+ s -= val ;
241
+ if (! left && ! right && s === 0 ) {
242
+ ans .push ([... t ]);
246
243
}
247
- paths .pop ();
244
+ dfs (left , s );
245
+ dfs (right , s );
246
+ t .pop ();
248
247
};
248
+
249
249
dfs (root , target );
250
- return res ;
250
+ return ans ;
251
251
}
252
252
```
253
253
@@ -278,26 +278,26 @@ impl Solution {
278
278
fn dfs (
279
279
root : & Option <Rc <RefCell <TreeNode >>>,
280
280
mut target : i32 ,
281
- paths : & mut Vec <i32 >,
282
- res : & mut Vec <Vec <i32 >>
281
+ t : & mut Vec <i32 >,
282
+ ans : & mut Vec <Vec <i32 >>
283
283
) {
284
284
if let Some (node ) = root . as_ref () {
285
285
let node = node . borrow ();
286
- paths . push (node . val);
286
+ t . push (node . val);
287
287
target -= node . val;
288
288
if node . left. is_none () && node . right. is_none () && target == 0 {
289
- res . push (paths . clone ());
289
+ ans . push (t . clone ());
290
290
}
291
- Self :: dfs (& node . left, target , paths , res );
292
- Self :: dfs (& node . right, target , paths , res );
293
- paths . pop ();
291
+ Self :: dfs (& node . left, target , t , ans );
292
+ Self :: dfs (& node . right, target , t , ans );
293
+ t . pop ();
294
294
}
295
295
}
296
296
297
297
pub fn path_sum (root : Option <Rc <RefCell <TreeNode >>>, target : i32 ) -> Vec <Vec <i32 >> {
298
- let mut res = vec! [];
299
- Self :: dfs (& root , target , & mut vec! [], & mut res );
300
- res
298
+ let mut ans = vec! [];
299
+ Self :: dfs (& root , target , & mut vec! [], & mut ans );
300
+ ans
301
301
}
302
302
}
303
303
```
@@ -325,13 +325,14 @@ var pathSum = function (root, target) {
325
325
if (! root) {
326
326
return ;
327
327
}
328
- t .push (root .val );
329
- s -= root .val ;
330
- if (! root .left && ! root .right && ! s) {
328
+ const { val , left , right } = root;
329
+ t .push (val);
330
+ s -= val;
331
+ if (! left && ! right && ! s) {
331
332
ans .push ([... t]);
332
333
}
333
- dfs (root . left , s);
334
- dfs (root . right , s);
334
+ dfs (left, s);
335
+ dfs (right, s);
335
336
t .pop ();
336
337
};
337
338
dfs (root, target);
@@ -384,78 +385,4 @@ public class Solution {
384
385
385
386
<!-- solution: end -->
386
387
387
- <!-- solution: start-- >
388
-
389
- ### 方法二
390
-
391
- <!-- tabs: start -->
392
-
393
- #### Rust
394
-
395
- ``` rust
396
- // Definition for a binary tree node.
397
- // #[derive(Debug, PartialEq, Eq)]
398
- // pub struct TreeNode {
399
- // pub val: i32,
400
- // pub left: Option<Rc<RefCell<TreeNode>>>,
401
- // pub right: Option<Rc<RefCell<TreeNode>>>,
402
- // }
403
- //
404
- // impl TreeNode {
405
- // #[inline]
406
- // pub fn new(val: i32) -> Self {
407
- // TreeNode {
408
- // val,
409
- // left: None,
410
- // right: None
411
- // }
412
- // }
413
- // }
414
- use std :: rc :: Rc ;
415
- use std :: cell :: RefCell ;
416
- impl Solution {
417
- fn dfs (
418
- root : & Option <Rc <RefCell <TreeNode >>>,
419
- mut target : i32 ,
420
- paths : & mut Vec <i32 >
421
- ) -> Vec <Vec <i32 >> {
422
- let node = root . as_ref (). unwrap (). borrow ();
423
- paths . push (node . val);
424
- target -= node . val;
425
- let mut res = vec! [];
426
- // 确定叶结点身份
427
- if node . left. is_none () && node . right. is_none () {
428
- if target == 0 {
429
- res . push (paths . clone ());
430
- }
431
- } else {
432
- if node . left. is_some () {
433
- let res_l = Self :: dfs (& node . left, target , paths );
434
- if ! res_l . is_empty () {
435
- res = [res , res_l ]. concat ();
436
- }
437
- }
438
- if node . right. is_some () {
439
- let res_r = Self :: dfs (& node . right, target , paths );
440
- if ! res_r . is_empty () {
441
- res = [res , res_r ]. concat ();
442
- }
443
- }
444
- }
445
- paths . pop ();
446
- res
447
- }
448
- pub fn path_sum (root : Option <Rc <RefCell <TreeNode >>>, target : i32 ) -> Vec <Vec <i32 >> {
449
- if root . is_none () {
450
- return vec! [];
451
- }
452
- Self :: dfs (& root , target , & mut vec! [])
453
- }
454
- }
455
- ```
456
-
457
- <!-- tabs: end -->
458
-
459
- <!-- solution: end -->
460
-
461
388
<!-- problem: end -->
0 commit comments