1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// @Title: 两数之和 IV - 输入 BST (Two Sum IV - Input is a BST)
// @Author: 15816537946@163.com
// @Date: 2022-03-21 16:28:42
// @Runtime: 20 ms
// @Memory: 7.2 MB
/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func findTarget(root *TreeNode, k int) bool {
	c := make(map[int]struct{})
	var dfs func(*TreeNode) bool
	dfs = func(node *TreeNode) bool {
		if node == nil {
			return false
		}
		if _, ok := c[k-node.Val]; ok {
			return true
		}
		c[node.Val] = struct{}{}
		return dfs(node.Left) || dfs(node.Right)
	}

	return dfs(root)

}