Advertisement
Guest User

Untitled

a guest
Oct 27th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. class Result {
  2. public:
  3. Result(bool isBST, int min, int max) {
  4. this->isBST = isBST;
  5. this->min = min;
  6. this->max = max;
  7. }
  8. bool isBST;
  9. int min;
  10. int max;
  11. };
  12.  
  13. bool checkBST(Node* root) {
  14. if (root == NULL)
  15. return true;
  16. else
  17. return check(root)->isBST;
  18. }
  19.  
  20. Result* check(Node* node) {
  21. int min = node->data;
  22. int max = node->data;
  23.  
  24. if (node->left != NULL) {
  25. Result* leftResult = check(node->left);
  26. if (!leftResult->isBST || leftResult->max >= node->data)
  27. return new Result(false, 0, 0);
  28. min = leftResult->min;
  29. delete leftResult;
  30. }
  31.  
  32. if (node->right != NULL) {
  33. Result* rightResult = check(node->right);
  34. if (!rightResult->isBST || rightResult->min <= node->data)
  35. return new Result(false, 0, 0);
  36. max = rightResult->max;
  37. delete rightResult;
  38. }
  39. return new Result(true, min, max);
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement