Amazon Interview Question

The interviewer wanted me to create a structure that represents a binary search tree and write a boolean function that verified to see whether a parameter was a binary search tree.

Interview Answer

Anonymous

Jul 18, 2012

A binary search tree requires the left subtree to be less than the parent node and the right subtree to be greater than the parent node. Therefore, the correct answer is to create a method with three parameters, the "tree" you're verifying, a minimum value, and a maximum value. These values are set depending on what node you're comparing, such that the maximum value is set whenever you're traversing the right subtrees, and the minimum value is set whenever you're traversing the left subtrees.