Skip to content

Instantly share code, notes, and snippets.

@CollinShoop
Last active January 23, 2022 04:31
Show Gist options
  • Save CollinShoop/501d54554ebd67e65c363ae973d8ebe0 to your computer and use it in GitHub Desktop.
Save CollinShoop/501d54554ebd67e65c363ae973d8ebe0 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 findTarget(root *TreeNode, k int) bool {
vals := map[int]bool{}
var helper func(node *TreeNode) bool
helper = func(node *TreeNode) bool {
if node == nil {
return false
}
// Note: It's important to check for k-node.Val first, in the case that node.Val == k/2
if vals[k-node.Val] {
return true
}
vals[node.Val] = true
return helper(node.Left) || helper(node.Right)
}
return helper(root)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment