-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathisBtBST.cpp
More file actions
59 lines (55 loc) · 1.33 KB
/
isBtBST.cpp
File metadata and controls
59 lines (55 loc) · 1.33 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include<bits/stdc++.h>
using namespace std;
struct Node {
char data;
Node*left;
Node*right;
};
Node* root = NULL;
struct Node* insert(struct Node* root, char x) {
struct Node* temp = (struct Node*)malloc(sizeof(struct Node*));
temp->data = x;
temp->left = NULL;
temp->right = NULL;
if(root == NULL) {
root = temp;
}
else if(x <= root->data) {
root->left = insert(root->left, x);
} else {
root->right = insert(root->right, x);
}
return root;
}
bool isBstUtil(Node* root, int minValue, int maxValue) {
if(root == NULL) {
return true;
}
if(root->data > minValue && root->data < maxValue && isBstUtil(root->left, minValue, root->data) && isBstUtil(root->right, root->data, maxValue)) {
return true;
}
else {
return false;
}
}
bool IsBST(Node* root) {
return (isBstUtil(root, INT_MIN, INT_MAX));
}
int main() {
root = insert(root,'F');
root = insert(root,'D');
root = insert(root,'J');
root = insert(root,'B');
root = insert(root,'E');
root = insert(root,'A');
root = insert(root,'C');
root = insert(root,'G');
root = insert(root,'K');
root = insert(root,'I');
root = insert(root,'H');
if(IsBST(root)) {
cout<<"Yes"<<endl;
} else {
cout<<"No"<<endl;
}
}