-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path312.py
More file actions
27 lines (24 loc) · 743 Bytes
/
312.py
File metadata and controls
27 lines (24 loc) · 743 Bytes
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
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def findTarget(self, root: Optional[TreeNode], k: int) -> bool:
self.visited = dict()
self.result = False
self.dfs(root, k)
return self.result
def dfs(self, root, k):
if root is None:
return
if self.result == True:
return
val = k - root.val
if not self.visited.get(val, False):
self.visited[root.val] = True
self.dfs(root.left, k)
self.dfs(root.right, k)
else:
self.result = True