Q: How do you determine if a binary tree is binary search tree ?
A: bool IsBST( node* root, int* prev)
{
bool ret = true;
if (root == null) return ret;
ret = IsBST(root->left, prev);
if (ret==false) return ret;
if (*prev >= root->val) return false;
*prev = root->val;
ret = IsBST(root->right, prev);
if (ret==false) return ret;
return ret;
}
A: bool IsBST( node* root, int* prev)
{
bool ret = true;
if (root == null) return ret;
ret = IsBST(root->left, prev);
if (ret==false) return ret;
if (*prev >= root->val) return false;
*prev = root->val;
ret = IsBST(root->right, prev);
if (ret==false) return ret;
return ret;
}
No comments:
Post a Comment