Amazon Interview Question

Code to find median in BST

Interview Answers

Anonymous

Jan 20, 2011

Where n = number of elements in the BST, use an in-order Depth first search to traverse the tree and return element n/2 + 1. (The +1 is needed, assuming that n is an odd number and there is a true single median value.)

1

Anonymous

Nov 16, 2010

Step 1) Do a Breadth first search/dump to count the number of elements. Step 2) Count half of that in your second breadth first dump and return the element.