-
Notifications
You must be signed in to change notification settings - Fork 3
/
36_Dropbox_Find_Second_Largest_Node_in_BST.py
executable file
·157 lines (116 loc) · 4.01 KB
/
36_Dropbox_Find_Second_Largest_Node_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
"""
This problem was asked by Dropbox.
Given the root to a binary search tree, find the second largest node in the tree.
eg.
5
/ \
2 6
/ \ \
1 4 9
/ \
3 12
/
11
ANS = 11
eg.
5
/ \
2 8
ANS = 5
eg. 6
/
4
/ \
3 5
ANS = 5
NOTICE: ANS is either:
1- the root-node,
2- in the right sub-tree from the root-node, if it exists
3- in the left subtree from the root node.
"""
class node:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right= right
def create_my_tree(self): # ANS False
self.data = 4
self.left = node(2)
self.right= node(6)
self.left.left=node(1)
self.left.right = node(2)
self.right.left = node(5)
self.right.right = node(7)
def print_node(self):
print(self.data)
def print_inorder(self):
if self.left: # left exists
self.left.print_inorder()
print(self.data)
if self.right:
self.right.print_inorder()
def insert(self, data):
if (data <= self.data): # add to the left
if self.left == None: # left empty
self.left = node(data)
else: # left exists
self.left.insert(data)
else: # add to the right
if self.right == None: # right empty
self.right = node(data)
else: # right exists
self.right.insert(data)
def find_second_largest(root:node):
arr = [] # stores in the 0th position the second largest element and in the 1st position the largest one
arr.append(root.data) # the 1st condition
# if right subtree exists
if root.right:
def search_right(current_node):
if current_node.left: # left exists -> go left
search_right(current_node.left)
if len(arr) == 1:
if current_node.data < arr[0]:
arr.insert(0, current_node.data)
else:
arr.insert(1, current_node.data)
elif len(arr) == 2:
if current_node.data > arr[1]:
arr[0], arr[1] = arr[1], arr[0]
arr[1] = current_node.data
if current_node.right: # left exists -> go left
search_right(current_node.right)
current_node = root.right
search_right(current_node)
return arr[0]
# only left subtree exists
def search_left(current_node):
if current_node.left: # left exists -> go left
search_left(current_node.left)
# check if smaller or bigger
if len(arr) ==1:
if current_node.data < arr[0]:
arr.insert(0, current_node.data)
else:
arr.insert(1, current_node.data)
elif len(arr) == 2:
# in the left subtree the elements can't be bigger than the root
if current_node.data > arr[0]:
arr[0]=current_node.data
if current_node.right:
search_left(current_node.right)
current_node = root.left
search_left(current_node)
return arr[0]
if __name__ == '__main__':
# tree = node(5,
# left=node(2, left=node(1), right=node(4, left=node(3))),
# right=node(6, right=node(9, right=node(12, left=node(11))))) # ans 11
# tree = node(5,
# left=node(2),
# right=node(8)) # ans 5
# tree = node(6,
# left=node(4, left=node(3), right=node(5))) # ans 5
tree = node(9,
left=node(4, left=node(1)),
right=node(7, right=node(31, left=node(14),right=node(82)))) # ans 31
print(find_second_largest(tree))