-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckifbst.cpp
More file actions
54 lines (45 loc) · 919 Bytes
/
checkifbst.cpp
File metadata and controls
54 lines (45 loc) · 919 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
45
46
47
48
49
50
51
52
53
54
#include<bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node*left;
node*right;
node(int data)
{
this->data = data;
this->left = NULL;
this->right = NULL;
}
};
int isBSTUtil(node*node,int min,int max)
{
if(node==NULL)
{
return 1;
}
if(node->data < min || node->data > max)
{
return 0;
}
return isBSTUtil(node->left,min,node->data-1);
return isBSTUtil(node->right,node->data+1,max);
}
int isBST(node*node)
{
return (isBSTUtil(node,INT_MIN,INT_MAX));
}
int main()
{
node*root = new node(4);
root->left = new node(2);
root->right = new node(5);
root->left->left = new node(1);
root->left->right = new node(3);
if(isBST(root))
cout<<"Is BST";
else
cout<<"Not a BST";
return 0;
}