-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem36.py
More file actions
29 lines (22 loc) · 869 Bytes
/
Copy pathproblem36.py
File metadata and controls
29 lines (22 loc) · 869 Bytes
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
# This problem was asked by Dropbox.
# Given the root to a binary search tree,
# find the second largest node in the tree.
from data_structures import BT_Node, build_binary_tree, max_bst_node
def second_largest(root):
if not root:
raise Exception('Invalid root node')
if not root.right:
return max_bst_node(root.left)
if root.right.right:
# print('switching to ' + str(root.right.data))
return second_largest(root.right)
if root.right.left:
# print('comparing ' + root.data + max_bst_node(root.right.left))
return max(root.data, max_bst_node(root.right.left))
else:
return root.data
if __name__ == "__main__":
node_values = [8,3,10,1,6,None,14,None,None,4,7,None,None,13]
root = build_binary_tree(node_values, 0)
print(root)
print(second_largest(root))