forked from mukul96/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Validate Binary Search Tree problem
- Loading branch information
Showing
1 changed file
with
32 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
}; |