This repository was archived by the owner on Sep 20, 2023. It is now read-only.
File tree Expand file tree Collapse file tree 1 file changed +11
-34
lines changed
Algorithms/1080.insufficient-nodes-in-root-to-leaf-paths Expand file tree Collapse file tree 1 file changed +11
-34
lines changed Original file line number Diff line number Diff line change 1
1
package problem1080
2
2
3
3
import (
4
- "math"
5
-
6
4
"github.com/aQuaYi/LeetCode-in-Go/kit"
7
5
)
8
6
@@ -15,41 +13,20 @@ import (
15
13
type TreeNode = kit.TreeNode
16
14
17
15
func sufficientSubset (root * TreeNode , limit int ) * TreeNode {
18
- if dfs ( root , 0 , limit ) < limit {
16
+ if root == nil {
19
17
return nil
20
18
}
21
- return root
22
- }
23
-
24
- func dfs (node * TreeNode , pre , limit int ) int {
25
- if node .Left == nil && node .Right == nil {
26
- return node .Val
27
- }
28
-
29
- pre += node .Val
30
-
31
- l , r := math .MinInt64 , math .MinInt64
32
-
33
- if node .Left != nil {
34
- l = dfs (node .Left , pre , limit )
35
- if pre + l < limit {
36
- node .Left = nil
37
- }
38
- }
39
-
40
- if node .Right != nil {
41
- r = dfs (node .Right , pre , limit )
42
- if pre + r < limit {
43
- node .Right = nil
19
+ if root .Left == nil && root .Right == nil {
20
+ if root .Val < limit {
21
+ return nil
44
22
}
23
+ return root
45
24
}
46
-
47
- return max (l , r ) + node .Val
48
- }
49
-
50
- func max (a , b int ) int {
51
- if a > b {
52
- return a
25
+ limit -= root .Val
26
+ root .Left = sufficientSubset (root .Left , limit )
27
+ root .Right = sufficientSubset (root .Right , limit )
28
+ if root .Left == root .Right { // both nil
29
+ return nil
53
30
}
54
- return b
31
+ return root
55
32
}
You can’t perform that action at this time.
0 commit comments