tvl-depot/users/wpcarro/scratch/facebook/second-largest-item-in-bst.py
Vincent Ambo 019f8fd211 subtree(users/wpcarro): docking briefcase at '24f5a642'
git-subtree-dir: users/wpcarro
git-subtree-mainline: 464bbcb15c
git-subtree-split: 24f5a642af
Change-Id: I6105b3762b79126b3488359c95978cadb3efa789
2021-12-14 02:15:47 +03:00

22 lines
500 B
Python

from collections import deque
from node import Node, tree
def find_largest(node):
while node.right:
node = node.right
return node.value
def find_second_largest(node):
# parent of the rightmost, when rightmost is leaf
# max(rightmost.left)
prev = None
while node.right:
prev = node
node = node.right
if node.left:
return find_largest(node.left)
else:
return prev.value
assert find_second_largest(tree) == 72
print("Success!")