Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit be7eeb9

Browse files
committed
1080 done
1 parent 1cef638 commit be7eeb9

File tree

1 file changed

+11
-34
lines changed

1 file changed

+11
-34
lines changed
Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package problem1080
22

33
import (
4-
"math"
5-
64
"github.com/aQuaYi/LeetCode-in-Go/kit"
75
)
86

@@ -15,41 +13,20 @@ import (
1513
type TreeNode = kit.TreeNode
1614

1715
func sufficientSubset(root *TreeNode, limit int) *TreeNode {
18-
if dfs(root, 0, limit) < limit {
16+
if root == nil {
1917
return nil
2018
}
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
4422
}
23+
return root
4524
}
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
5330
}
54-
return b
31+
return root
5532
}

0 commit comments

Comments
 (0)