File tree Expand file tree Collapse file tree 2 files changed +20
-23
lines changed Expand file tree Collapse file tree 2 files changed +20
-23
lines changed Original file line number Diff line number Diff line change 16
16
// }
17
17
// }
18
18
// }
19
- use std:: cell:: RefCell ;
20
19
use std:: rc:: Rc ;
20
+ use std:: cell:: RefCell ;
21
21
22
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 > > {
23
+ fn dfs ( root : & Option < Rc < RefCell < TreeNode > > > , mut target : i32 , paths : & mut Vec < i32 > ) -> Vec < Vec < i32 > > {
28
24
let node = root. as_ref ( ) . unwrap ( ) . borrow ( ) ;
29
- values . push ( node. val ) ;
25
+ paths . push ( node. val ) ;
30
26
target -= node. val ;
31
27
let mut res = vec ! [ ] ;
32
28
// 确定叶结点身份
33
29
if node. left . is_none ( ) && node. right . is_none ( ) {
34
30
if target == 0 {
35
- res. push ( values ) ;
31
+ res. push ( paths . clone ( ) ) ;
36
32
}
33
+ paths. pop ( ) ;
37
34
return res;
38
35
}
39
36
if node. left . is_some ( ) {
40
- let res_l = Solution :: dfs ( & node. left , target, values . clone ( ) ) ;
37
+ let res_l = Solution :: dfs ( & node. left , target, paths ) ;
41
38
if !res_l. is_empty ( ) {
42
39
res = [ res, res_l] . concat ( ) ;
43
40
}
44
41
}
45
42
if node. right . is_some ( ) {
46
- let res_r = Solution :: dfs ( & node. right , target, values . clone ( ) ) ;
43
+ let res_r = Solution :: dfs ( & node. right , target, paths ) ;
47
44
if !res_r. is_empty ( ) {
48
45
res = [ res, res_r] . concat ( ) ;
49
46
}
50
47
}
48
+ paths. pop ( ) ;
51
49
res
52
50
}
53
51
pub fn path_sum ( root : Option < Rc < RefCell < TreeNode > > > , target : i32 ) -> Vec < Vec < i32 > > {
54
52
if root. is_none ( ) {
55
53
return vec ! [ ] ;
56
54
}
57
- Solution :: dfs ( & root, target, vec ! [ ] )
55
+ Solution :: dfs ( & root, target, & mut vec ! [ ] )
58
56
}
59
- }
57
+ }
Original file line number Diff line number Diff line change 13
13
*/
14
14
15
15
function pathSum ( root : TreeNode | null , target : number ) : number [ ] [ ] {
16
- const res = [ ] ;
16
+ const res : number [ ] [ ] = [ ] ;
17
17
if ( root == null ) {
18
18
return res ;
19
19
}
20
- const dfs = (
21
- { val, right, left } : TreeNode ,
22
- target : number ,
23
- values : number [ ]
24
- ) => {
25
- values . push ( val ) ;
20
+ const paths : number [ ] = [ ] ;
21
+ const dfs = ( { val, right, left } : TreeNode , target : number ) => {
22
+ paths . push ( val ) ;
26
23
target -= val ;
27
24
if ( left == null && right == null ) {
28
25
if ( target === 0 ) {
29
- res . push ( values ) ;
26
+ res . push ( [ ... paths ] ) ;
30
27
}
28
+ paths . pop ( ) ;
31
29
return ;
32
30
}
33
- left && dfs ( left , target , [ ...values ] ) ;
34
- right && dfs ( right , target , [ ...values ] ) ;
31
+ left && dfs ( left , target ) ;
32
+ right && dfs ( right , target ) ;
33
+ paths . pop ( ) ;
35
34
} ;
36
- dfs ( root , target , [ ] ) ;
35
+ dfs ( root , target ) ;
37
36
return res ;
38
37
}
You can’t perform that action at this time.
0 commit comments