forked from ngiengkianyew/daily-coding-problem
-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem_125.py
52 lines (41 loc) · 954 Bytes
/
problem_125.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
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
def __repr__(self):
return str(self.val)
def get_array(root):
arr = [root]
if not root.left and not root.right:
return arr
if root.left:
arr = get_array(root.left) + arr
if root.right:
arr = arr + get_array(root.right)
return arr
def search_pair(root, val):
arr = get_array(root)
i, k = 0, len(arr) - 1
while i < k:
summed = arr[i].val + arr[k].val
if summed == val:
return (arr[i], arr[k])
elif summed < val:
i += 1
else:
k -= 1
# Tests
a = Node(10)
b = Node(5)
c = Node(15)
d = Node(11)
e = Node(15)
a.left = b
a.right = c
c.left = d
c.right = e
assert search_pair(a, 15) == (b, a)
assert search_pair(a, 20) == (b, e)
assert search_pair(a, 30) == (c, e)
assert search_pair(a, 26) == (d, e)