Skip to content

Instantly share code, notes, and snippets.

@RP-3
Created August 10, 2020 02:05
Show Gist options
  • Save RP-3/73d40f3dfe5f49c286e2cf1cc90207ae to your computer and use it in GitHub Desktop.
Save RP-3/73d40f3dfe5f49c286e2cf1cc90207ae to your computer and use it in GitHub Desktop.
package main
// TreeNode Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
func _pathSum(root *TreeNode, remainder, target int, memo map[int]int) int {
if root == nil {
return 0
}
remainder += root.Val
result := memo[remainder-target]
memo[remainder]++
result += _pathSum(root.Left, remainder, target, memo)
result += _pathSum(root.Right, remainder, target, memo)
memo[remainder]--
return result
}
func pathSum(root *TreeNode, sum int) int {
m := map[int]int{0: 1}
return _pathSum(root, 0, sum, m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment