-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhigh_low_value_in_binary_tree.py
50 lines (41 loc) · 1.5 KB
/
high_low_value_in_binary_tree.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
"""
Hi, here's your problem today. This problem was recently asked by Apple:
Given an integer k and a binary search tree, find the floor (less than or
equal to) of k, and the ceiling (larger than or equal to) of k. If either
does not exist, then print them as None.
Here is the definition of a node for the tree.
"""
class Node:
def __init__(self, value):
self.left = None
self.right = None
self.value = value
def findCeilingFloor(node, k, floor=None, ceil=None):
# first call only
if ceil is None and node.value > k:
ceil = node.value
if floor is None and node.value < k:
floor = node.value
# second and later calls
if ceil is not None and node.value < ceil and node.value > k:
ceil = node.value
if floor is not None and node.value > floor and node.value < k:
floor = node.value
if node.left is not None:
floor, ceil = findCeilingFloor(node.left, k, floor, ceil)
if node.right is not None:
floor, ceil = findCeilingFloor(node.right, k, floor, ceil)
return (floor, ceil)
if __name__ == '__main__':
root = Node(10)
root.left = Node(2)
root.left.left = Node(7)
root.left.left.left = Node(1)
root.left.left.right = Node(16)
root.left.right = Node(9)
root.right = Node(13)
root.right.left = Node(3)
root.right.right = Node(3)
assert findCeilingFloor(root, 5) == (3, 7)
assert findCeilingFloor(root, 12) == (10, 13)
assert findCeilingFloor(root, 3) == (2, 7)