Files
LeetCode-Go/leetcode/0653.Two-Sum-IV---Input-is-a-BST/653. Two Sum IV - Input is a BST.go
2020-08-07 17:06:53 +08:00

34 lines
619 B
Go

package leetcode
import (
"github.com/halfrost/LeetCode-Go/structures"
)
// TreeNode define
type TreeNode = structures.TreeNode
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func findTarget(root *TreeNode, k int) bool {
m := make(map[int]int, 0)
return findTargetDFS(root, k, m)
}
func findTargetDFS(root *TreeNode, k int, m map[int]int) bool {
if root == nil {
return false
}
if _, ok := m[k-root.Val]; ok {
return ok
}
m[root.Val]++
return findTargetDFS(root.Left, k, m) || findTargetDFS(root.Right, k, m)
}