Interview Questions

168. How do you know if a tree is a bst or not ?

Microsoft Interview Questions and Answers


(Continued from previous question...)

168. How do you know if a tree is a bst or not ?

Question:
How do you know if a tree is a bst or not


maybe an answer:


bool isBST(node *root)
{
if(root == NULL)
return true;
else if(root->left == NULL && root->right == NULL)
return true;
else
{
node *travLeft = root->left;
while(travLeft->right)
travLeft = travLeft->right;

node *travRight = root->right;
while(travRight->left)
travRight = travRight->left;

if(travLeft->data <= root->data &&
travRight->data > root->data &&
isBST(root->left) &&
isBST(root->right))
return true;
else
return false;
}
}

(Continued on next question...)

Other Interview Questions