-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_binary_search_tree.cpp
More file actions
44 lines (35 loc) · 856 Bytes
/
Copy pathvalidate_binary_search_tree.cpp
File metadata and controls
44 lines (35 loc) · 856 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include<iostream>
#include<climits>
using namespace std;
class BinaryTreeNode {
public:
int data;
BinaryTreeNode* left;
BinaryTreeNode* right;
BinaryTreeNode(int data) {
this->data = data;
left = NULL;
right = NULL;
}
~BinaryTreeNode() {
delete left;
delete right;
}
};
bool isBST (BinaryTreeNode* root, int min = INT_MIN, int max = INT_MAX) {
if (root == NULL)
return true;
if (root->data < min || root->data > max)
return false;
bool isLeftOk = isBST(root->left, min, root->data - 1);
bool isRightOk = isBST(root->right, root->data, max);
return isLeftOk && isRightOk;
}
int main () {
BinaryTreeNode* root = new BinaryTreeNode(4);
root->left = new BinaryTreeNode(3);
root->right = new BinaryTreeNode(5);
root->left->left = new BinaryTreeNode(1);
isBST(root) ? cout << "YES" : cout << "NO";
return 0;
}