-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst_2_nodes_sum_to_k.py
66 lines (56 loc) · 1.54 KB
/
bst_2_nodes_sum_to_k.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
from collections import deque
#Iterative inorder traversal of a BST. Useless for this code.
def in_order_iter(root):
stack = deque()
curr = root
while True:
while curr:
stack.appendleft(curr)
curr = curr.left
if len(stack):
curr = stack.popleft()
print curr.value
curr = curr.right
else:
break
class Node:
def __init__(self, value, left, right):
self.right = right
self.left = left
self.value = value
def construct_bst_from_sorted_array(a, i, j):
if j == i:
return None
mid = (i + j) / 2
return Node(
a[mid],
construct_bst_from_sorted_array(a, i, mid),
construct_bst_from_sorted_array(a, mid + 1, j)
)
def order(asc = True):
def in_order(node):
if node:
small = node.left if asc else node.right
large = node.right if asc else node.left
for value in in_order(small):
yield value
yield node.value
for value in in_order(large):
yield value
return in_order
a = [1, 6, 7, 15, 19, 21, 28]
tree = construct_bst_from_sorted_array(a, 0, len(a))
k = 26
asc_in_order = order(True)(tree)
dsc_in_order = order(False)(tree)
left = asc_in_order.next()
right = dsc_in_order.next()
while True:
print left, right
if left + right == k:
print left, right
break
elif left + right < k:
left = asc_in_order.next()
else:
right = dsc_in_order.next()