Skip to content

Instantly share code, notes, and snippets.

@CollinShoop
Created January 23, 2022 04:01
Show Gist options
  • Save CollinShoop/fb434e5eafbf9b118b0f92e40c71623f to your computer and use it in GitHub Desktop.
Save CollinShoop/fb434e5eafbf9b118b0f92e40c71623f to your computer and use it in GitHub Desktop.
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func hasPathSum(root *TreeNode, targetSum int) bool {
var helper func(root *TreeNode, sum int) bool
helper = func(root *TreeNode, sum int) bool {
if root == nil {
return false
}
sum += root.Val
// check sum for leaf
if sum == targetSum && root.Left == nil && root.Right == nil {
return true
}
return helper(root.Left, sum) || helper(root.Right, sum)
}
return helper(root, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment