-
Notifications
You must be signed in to change notification settings - Fork 3
/
133_Amazon_Find_Inorder_Successor_In_BST.py
executable file
·55 lines (40 loc) · 1.27 KB
/
133_Amazon_Find_Inorder_Successor_In_BST.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
"""
This problem was asked by Amazon.
Given a node in a binary search tree, return the next bigger element, also known as the inorder successor.
For example, the inorder successor of 22 is 30.
10
/ \
5 30
/ \
22 35
You can assume each node has a parent pointer.
"""
class Node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def __repr__(self):
return str(self.data)
def naive_succsessor(root, target, prev=None):
inorder = []
def get_inorder_traveral(r):
nonlocal inorder
if r.left:
get_inorder_traveral(r.left)
inorder.append(r.data)
if r.right:
get_inorder_traveral(r.right)
get_inorder_traveral(root)
try:
successor_index = inorder.index(target) +1
return inorder[successor_index] if successor_index < len(inorder) else None
except ValueError:
return None
if __name__ == '__main__':
root = Node(10, left=Node(5), right=Node(30, left=Node(22), right=Node(35)))
print(naive_succsessor(root, 22))
print(naive_succsessor(root, 5))
print(naive_succsessor(root, 10))
print(naive_succsessor(root, 35))
print(naive_succsessor(root, 355)) # value error