Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Result {
- public:
- Result(bool isBST, int min, int max) {
- this->isBST = isBST;
- this->min = min;
- this->max = max;
- }
- bool isBST;
- int min;
- int max;
- };
- bool checkBST(Node* root) {
- if (root == NULL)
- return true;
- else
- return check(root)->isBST;
- }
- Result* check(Node* node) {
- int min = node->data;
- int max = node->data;
- if (node->left != NULL) {
- Result* leftResult = check(node->left);
- if (!leftResult->isBST || leftResult->max >= node->data)
- return new Result(false, 0, 0);
- min = leftResult->min;
- delete leftResult;
- }
- if (node->right != NULL) {
- Result* rightResult = check(node->right);
- if (!rightResult->isBST || rightResult->min <= node->data)
- return new Result(false, 0, 0);
- max = rightResult->max;
- delete rightResult;
- }
- return new Result(true, min, max);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement