-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvaidateBst.cpp
38 lines (34 loc) · 895 Bytes
/
vaidateBst.cpp
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
/**
LeetCode 98 - validate BST
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
/*
// approach 1 -> deserialize the bst and verify arr[i] > arr[i-1]
// approach 2 ->
// inorder traversal while maintaining min and max
// if this violates,return false,else return true
*/
class Solution {
public:
bool solve(TreeNode* root,long long int min,long long int max)
{
if(root == NULL)
{
return true;
}
if(root->val < min || root->val > max)
{
return false;
}
return solve(root->left,min,(long long int)root->val-1)&&solve(root->right,(long long int)root->val+1,max);
}
bool isValidBST(TreeNode* root) {
return solve(root,INT_MIN,INT_MAX);
}
};