Skip to content

Commit

Permalink
Validate BST
Browse files Browse the repository at this point in the history
  • Loading branch information
iisha27 committed Oct 22, 2022
1 parent 8ff36c1 commit 577e45c
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions leetcode/ValidateBST.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include<bits/stdc++.h>
using namespace std;
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:

bool isValid(TreeNode* root, long min, long max){
if(!root){
return true;
}
if(root->val>min && root->val<max){
return isValid(root->left, min, root->val) && isValid(root->right, root->val, max);
}
return false;

}
bool isValidBST(TreeNode* root) {
return isValid(root, LONG_MIN, LONG_MAX);
}
};

0 comments on commit 577e45c

Please sign in to comment.