Skip to content

Commit b572df8

Browse files
authored
Merge pull request neetcode-gh#1037 from julienChemillier/patch-6
Add 98 in c language
2 parents edcdf10 + 49aaf9d commit b572df8

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

c/98-Validate-Binary-Search-Tree.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
Given the root of a binary tree, determine if it is a valid binary search tree (BST).
3+
Time: O(n)
4+
Space: O(1)
5+
*/
6+
7+
#define MAX 9223372036854775807
8+
#define MIN -9223372036854775808
9+
10+
bool isSubTreeValid(struct TreeNode* root, long int lowerBound, long int upperBound) {
11+
if (root==NULL)
12+
return true;
13+
return root->val<upperBound && root->val>lowerBound && isSubTreeValid(root->left, lowerBound, root->val) && isSubTreeValid(root->right, root->val, upperBound);
14+
}
15+
16+
bool isValidBST(struct TreeNode* root){
17+
return isSubTreeValid(root, MIN, MAX);
18+
}

0 commit comments

Comments
 (0)