-
Notifications
You must be signed in to change notification settings - Fork 0
/
125.py
76 lines (57 loc) · 1.67 KB
/
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
"""
Problem:
Given the root of a binary search tree, and a target K, return two nodes in the tree
whose sum equals K.
For example, given the following tree and K of 20
10
/ \
5 15
/ \
11 15
Return the nodes 5 and 15.
"""
from typing import Generator, Optional, Tuple
from DataStructures.Tree import BinaryTree, Node
def inorder_traverse_generator(node: Node) -> Generator[int, None, None]:
if node.left:
for val in inorder_traverse_generator(node.left):
yield val
yield node.val
if node.right:
for val in inorder_traverse_generator(node.right):
yield val
def get_inorder_traverse_generator(
tree: BinaryTree,
) -> Optional[Generator[int, None, None]]:
if tree.root:
return inorder_traverse_generator(tree.root)
return None
def get_target_sum(tree: BinaryTree, k: int) -> Tuple[Optional[int], Optional[int]]:
generator = get_inorder_traverse_generator(tree)
if not generator:
return None, None
# checking for the target sum
previous = set()
for val in generator:
if (k - val) in previous:
return (k - val), val
previous.add(val)
return None, None
if __name__ == "__main__":
tree = BinaryTree()
tree.root = Node(10)
tree.root.left = Node(5)
tree.root.right = Node(15)
tree.root.right.left = Node(11)
tree.root.right.right = Node(15)
print(get_target_sum(tree, 15))
print(get_target_sum(tree, 20))
print(get_target_sum(tree, 21))
print(get_target_sum(tree, 25))
print(get_target_sum(tree, 30))
print(get_target_sum(tree, 35))
"""
SPECS:
TIME COMPLEXITY: O(n)
SPACE COMPLEXITY: O(n)
"""