Skip to content

Commit

Permalink
Time: 20 ms (29.97%), Space: 4.4 MB (72.21%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hovanhoa committed Oct 7, 2024
1 parent 43d699a commit 1cf00da
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions 0437-path-sum-iii/0437-path-sum-iii.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,23 @@
* }
*/

func dfs(root *TreeNode, targetSum int) int {
func dfs(root *TreeNode, targetSum int, total int) int {
if root == nil {
return 0
}

count := 0
if targetSum == root.Val {
count = 1
total += root.Val
if total == targetSum {
return 1 + dfs(root.Left, targetSum, total) + dfs(root.Right, targetSum, total)
}

return count + dfs(root.Left, targetSum - root.Val) + dfs(root.Right, targetSum - root.Val)
return dfs(root.Left, targetSum, total) + dfs(root.Right, targetSum, total)
}

func pathSum(root *TreeNode, targetSum int) int {
if root == nil {
return 0
}

return dfs(root, targetSum) + pathSum(root.Left, targetSum) + pathSum(root.Right, targetSum)
return dfs(root, targetSum, 0) + pathSum(root.Left, targetSum) + pathSum(root.Right, targetSum)
}

0 comments on commit 1cf00da

Please sign in to comment.