Amazon Interview Question

Write a function that takes a BinaryTree and validates if it is a valid BinarySearchTree. Assume the tree contains no duplicate values.

Interview Answers

Anonymous

Nov 3, 2011

Do an inorder traversal and check if the elements are sorted.

1

Anonymous

Jul 19, 2011

Write a recursive function that take the node as input and return three values: 1, If the tree rooted with this node is BST or not, 2, the max/min of the tree rooted with this node. If the node has children, the tree rooted with this node is a BST if: 1, Both its left and right children are BST 2, Max value of left < node < min value of right If the node has no childres, return (tree, node value, node value)