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

Commit 1cef638

Browse files
committed
1080 accepted.
1 parent 2800b6f commit 1cef638

File tree

1 file changed

+16
-19
lines changed

1 file changed

+16
-19
lines changed

Algorithms/1080.insufficient-nodes-in-root-to-leaf-paths/insufficient-nodes-in-root-to-leaf-paths.go

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,29 @@ func sufficientSubset(root *TreeNode, limit int) *TreeNode {
2222
}
2323

2424
func dfs(node *TreeNode, pre, limit int) int {
25-
if node == nil {
26-
return 0
25+
if node.Left == nil && node.Right == nil {
26+
return node.Val
2727
}
2828

2929
pre += node.Val
30-
l := dfs(node.Left, pre, limit)
31-
if pre+l < limit {
32-
node.Left = nil
33-
}
34-
r := dfs(node.Right, pre, limit)
35-
if pre+r < limit {
36-
node.Right = nil
37-
}
3830

39-
max := math.MinInt64
40-
if node.Left != nil && max < l {
41-
max = l
42-
}
43-
if node.Right != nil && max < r {
44-
max = r
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+
}
4538
}
4639

47-
if max == math.MinInt64 {
48-
return node.Val
40+
if node.Right != nil {
41+
r = dfs(node.Right, pre, limit)
42+
if pre+r < limit {
43+
node.Right = nil
44+
}
4945
}
50-
return max + node.Val
46+
47+
return max(l, r) + node.Val
5148
}
5249

5350
func max(a, b int) int {

0 commit comments

Comments
 (0)