``````

In [1]:

class BinaryTreeNode:

def __init__(self, value):
self.value = value
self.left  = None
self.right = None

def insert_left(self, value):
self.left = BinaryTreeNode(value)
return self.left

def insert_right(self, value):
self.right = BinaryTreeNode(value)
return self.right

``````
``````

In [50]:

def find_second_largest(tree):
largest = tree
second_largest = None

while tree.left is not None or tree.right is not None:
if tree.right is not None:
tree = tree.right
elif tree.left is not None:
tree = tree.left

if tree.value > largest.value:
second_largest, largest = largest, tree
elif tree.value > second_largest.value:
second_largest = tree

print 'I guess that the second largest value is %s' % (second_largest.value)
return second_largest.value

``````
``````

In [51]:

tree = BinaryTreeNode(100)
tree.left = BinaryTreeNode(0)
tree.right = BinaryTreeNode(200)

find_second_largest(tree) == 100

``````
``````

I guess that the second largest value is 100

Out[51]:

True

``````
``````

In [52]:

tree.right.left = BinaryTreeNode(101)

find_second_largest(tree) == 101

``````
``````

I guess that the second largest value is 101

Out[52]:

True

``````
``````

In [53]:

tree.right.right = BinaryTreeNode(300)

find_second_largest(tree) == 200

``````
``````

I guess that the second largest value is 200

Out[53]:

True

``````
``````

In [54]:

tree.right.right.left = BinaryTreeNode(201)
tree.right.right.right = BinaryTreeNode(400)
tree.right.right.right.right = BinaryTreeNode(500)
tree.right.right.right.right.left = BinaryTreeNode(401)

find_second_largest(tree) == 401

``````
``````

I guess that the second largest value is 401

Out[54]:

True

``````
``````

In [55]:

tree.right.right.right.right.left.right = BinaryTreeNode(402)

find_second_largest(tree) == 402

``````
``````

I guess that the second largest value is 402

Out[55]:

True

``````