Skip to content

Instantly share code, notes, and snippets.

@lowellbander
Created December 31, 2014 10:33
Show Gist options
  • Save lowellbander/d8c2bfcaebd8cbcdf755 to your computer and use it in GitHub Desktop.
Save lowellbander/d8c2bfcaebd8cbcdf755 to your computer and use it in GitHub Desktop.
Cracking the Coding Interview, Question 4.5: Implement a function to check if a binary tree is a binary search tree.
#include <climits>
// Cracking the Coding Interview, Question 4.5
// Implement a function to check if a binary tree is a binary search tree
bool isSearch(Node* root, int max = INT_MAX, int min = INT_MIN) {
if (!root) return true;
if (root->value <= min || root->value > max)
return false;
return isSearch(root->left, root->value, min) && isSearch(root->right, max, root->value);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment