Skip to content

Commit 65e9d15

Browse files
authored
98. Validate Binary Search Tree
1 parent 20365ae commit 65e9d15

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

98. Validate Binary Search Tree

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
4+
// T. c - O(n)
5+
// n times reverse kar rahe hai, isliye
6+
7+
bool solve(TreeNode* root,long long int lowerBound,long long int upperBound){
8+
if(root == NULL){
9+
return true;
10+
}
11+
bool cond1 = (root->val > lowerBound);
12+
bool cond2= (root->val < upperBound);
13+
bool leftAns = solve(root->left, lowerBound,root->val);
14+
bool rightAns = solve(root->right,root->val,upperBound);
15+
16+
if( cond1 && cond2 && leftAns && rightAns)
17+
return true;
18+
else
19+
return false;
20+
}
21+
bool isValidBST(TreeNode* root) {
22+
23+
long long int lowerBound = -2147483657; // we have to increase the limit
24+
long long int upperBound = 2147483657; // we have to increase the limit
25+
bool ans = solve(root,lowerBound,upperBound);
26+
return ans;
27+
28+
};

0 commit comments

Comments
 (0)