-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
Copy path653.py
47 lines (42 loc) · 1.49 KB
/
653.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
__________________________________________________________________________________________________
sample 64 ms submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findTarget(self, root: TreeNode, k: int) -> bool:
values = set()
return self.find(root, k, values)
def find(self, node, k, values):
if node is None:
return False
if k - node.val in values:
return True
values.add(node.val)
return self.find(node.left, k, values) or self.find(node.right, k, values)
__________________________________________________________________________________________________
sample 15348 kb submission
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def findTarget(self, root: TreeNode, k: int) -> bool:
if not root:
return False
queue, s = [root], set()
for i in queue:
if k - i.val in s:
return True
s.add(i.val)
if i.left:
queue.append(i.left)
if i.right:
queue.append(i.right)
return False
__________________________________________________________________________________________________