-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckBST.cpp
More file actions
44 lines (40 loc) · 752 Bytes
/
checkBST.cpp
File metadata and controls
44 lines (40 loc) · 752 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 <bits/stdc++.h>
using namespace std;
struct node
{
int data;
node *left;
node *right;
node(int x)
{
data = x;
left = NULL;
right = NULL;
}
};
int checkBST(node *root,int min1,int max1)
{
if(root == NULL)
return 1;
if(root->data<min1 || root->data>max1)
{
return 0;
}
int l = checkBST(root->left,min1,root->data-1);
int r = checkBST(root->right,root->data+1,max1);
int ans = l&r;
return ans;
}
int main()
{
int min = INT_MIN;
int max = INT_MAX;
node *head = new node(30);
head ->right = new node(40);
head->left = new node(20);
head->right->left = new node(35);
head->left->right = new node(25);
int x = checkBST(head,min,max);
cout<<x;
}