Skip to content

Commit

Permalink
added Validate Binary Search Tree problem
Browse files Browse the repository at this point in the history
  • Loading branch information
mukul96 authored Nov 25, 2022
1 parent a85f2f5 commit 6c2a2dd
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions Binary Search Tree/Validate Binary Search Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* 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:
void inorder(TreeNode *root, long long &prevValue, bool &flag){
if(root){
inorder(root->left, prevValue, flag);
if(prevValue>=(root->val)){
flag = false;
}
prevValue = (root->val);
inorder(root->right, prevValue, flag);
}
}
bool isValidBST(TreeNode* root) {
bool flag = true;
long long prevValue = LONG_MIN;
inorder(root, prevValue, flag);

return flag;

}
};

0 comments on commit 6c2a2dd

Please sign in to comment.