-
Notifications
You must be signed in to change notification settings - Fork 0
/
two-sum-iv-input-is-a-bst_653.py
75 lines (57 loc) · 1.84 KB
/
two-sum-iv-input-is-a-bst_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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# Given the root of a binary search tree and an integer k, return true if there exist two elements in the BST such that their sum is equal to k, or false otherwise.
# Example 1:
# 
# Input: root = [5,3,6,2,4,null,7], k = 9
# Output: true
# Example 2:
# 
# Input: root = [5,3,6,2,4,null,7], k = 28
# Output: false
# Constraints:
# The number of nodes in the tree is in the range [1, 104].
# -104 <= Node.val <= 104
# root is guaranteed to be a valid binary search tree.
# -105 <= k <= 105
# ---------------------------------------Runtime 94 ms Beats 14.66% Memory 19.5 MB Beats 75.52%---------------------------------------
from collections import deque
from typing import Optional
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:
nums = []
def dfs(node):
if node:
nums.append(node.val)
dfs(node.left)
dfs(node.right)
def binSearch(array: list[int], target):
start, end = 0, n - 2
while start <= end:
mid = (start + end) // 2
guess = array[mid]
if guess == target:
return True
if guess > target:
end = mid - 1
elif guess < target:
start = mid + 1
return False
dfs(root)
nums.sort()
n = len(nums)
res = False
nums = deque(nums)
i = 0
while i < n:
num = nums.pop()
target = k - num
res = binSearch(nums, target)
if res:
return True
i += 1
nums.appendleft(num)
return False