-
Notifications
You must be signed in to change notification settings - Fork 0
/
lesser_node.py
52 lines (45 loc) · 1.24 KB
/
lesser_node.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
'''
node_map = {}
def b_search(root, val):
if root.val == val:
return map_back(root)
if root.val < val and root.right:
add_to_map(root, root.right)
b_search(root.right)
elif root.val > val and root.left:
add_to_map(root, root.right)
b_search(root.left)
else:
return None
def add_to_map(parent, child):
node_map[child] = parent
def map_back(node):
while node in node_map and not node_map[node].val < node.val:
node = node_map[node]
if node == original_root:
return None
return node_map[node]
'''
original_root = None
def main(root, val):
original_root = root
return b_search(root, val)
def b_search(root, val):
if not root:
return None
if root.val == val:
return map_back(root)
if root.val < val and root.right:
return b_search(root.right, val)
elif root.val > val and root.left:
return b_search(root.left, val)
else:
return None
def add_to_map(parent, child):
node_map[child] = parent
def map_back(node):
while node.parent and node.parent.val > original_root.val:
node = node.parent
if node.parent and node.parent.val < original_root.val:
return node.parent
return None