Tuesday, November 1, 2011

How can we check whether a BT is a BST?

const int MIN_VALUE = -50000;
const int MAX_VALUE = 50000;

bool isValidBST( node *root) {
return isvalidHelper(root, MIN_VALUE, MAX_VALUE);
}

bool isvalidHelper(node *root, int min, int max) {
  if(root == null) return true;
if ( root->value < min || !isvalidHelper(root->left, min, root->value)
return false;
if ( root->value > max || !isvalidHelper(root->right, root->value, max)
return false;

return true;

}









No comments: