mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-08-03 02:47:26 +08:00
34 lines
619 B
Go
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)
|
|
}
|