Created
August 10, 2020 02:05
-
-
Save RP-3/73d40f3dfe5f49c286e2cf1cc90207ae to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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