forked from HarshithSimha/Compititive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path98_Validate_Binary_Search_Tree.cpp
More file actions
33 lines (26 loc) · 907 Bytes
/
98_Validate_Binary_Search_Tree.cpp
File metadata and controls
33 lines (26 loc) · 907 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
#include <iostream>
#include <bits/stdc++.h>
#include <cmath>
using namespace std;
class Solution {
public:
bool helper(TreeNode* root,long low ,long high){
// Empty trees are valid BSTs.
if(root == NULL){
return true;
}
// Condition of Binary Search Tree
// left side node values of the root should be less value than the root and right side should be greater
// If that is true check recursivley for the root child's
if((root->val < high) && (root->val > low)){
return (helper(root->left, low, root->val) && helper(root->right, root->val, high));
}else{
//if this root node violates the min/max constraint
return false;
}
}
bool isValidBST(TreeNode* root) {
bool ans = helper(root, LONG_MIN, LONG_MAX);
return ans;
}
};