two-sum-iv-input-is-a-bst

This commit is contained in:
Gleb Koval 2022-06-10 13:58:01 +01:00
parent d1d60eb34e
commit 93374f7414
Signed by: cyclane
GPG Key ID: 15E168A8B332382C
1 changed files with 31 additions and 0 deletions

View File

@ -0,0 +1,31 @@
package main
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func search(node *TreeNode, k int, visited *map[int]bool) bool {
if node == nil {
return false
}
if _, ok := (*visited)[k-node.Val]; ok {
return true
}
(*visited)[node.Val] = true
return search(node.Left, k, visited) || search(node.Right, k, visited)
}
func findTarget(root *TreeNode, k int) bool {
visited := map[int]bool{}
return search(root, k, &visited)
}