``````/**
* 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 isValidBST(TreeNode* root) {
vector<int> inorder;
inorder_bst(root,inorder);
vector<int> sorted(inorder);
sort(sorted.begin(),sorted.end());
vector<int>::iterator it = unique(sorted.begin(),sorted.end());
sorted.erase(it,sorted.end());
if(sorted.size() != inorder.size())
return false;

if (equal(inorder.begin(), inorder.begin() + inorder.size(), sorted.begin()))
return true;

return false;
}

private:
void inorder_bst(TreeNode * root,vector<int> & inorder)
{
if(root == NULL)
return;
inorder_bst(root->left,inorder);
inorder.push_back(root->val);
inorder_bst(root->right,inorder);
}
};``````

``````Runtime: 20 ms, faster than 45.02% of C++ online submissions for Validate Binary Search Tree.
Memory Usage: 22.3 MB, less than 93.69% of C++ online submissions for Validate Binary Search Tree.``````

``````/**
* 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 isValidBST(TreeNode* root) {
return isValidBST(root,NULL,NULL);
}
private:
bool isValidBST(TreeNode* root, TreeNode* minNode, TreeNode* maxNode) {
if(!root) return true;
if(minNode && root->val <= minNode->val ||
maxNode && root->val >= maxNode->val)
return false;

return isValidBST(root->left,minNode,root) && isValidBST(root->right,root,maxNode);
}
};``````

``````Runtime: 12 ms, faster than 93.30% of C++ online submissions for Validate Binary Search Tree.
Memory Usage: 22 MB, less than 93.69% of C++ online submissions for Validate Binary Search Tree.``````

``````/**
* 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 isValidBST(TreeNode* root) {
TreeNode * prev = NULL;
return validate(root,prev);
}
private:
bool validate(TreeNode* node, TreeNode* &prev) {
if(!node) return true;
if(!validate(node->left,prev)) return false;
if(prev && prev->val >= node->val) return false;
prev = node;
return validate(node->right,prev);
}
};``````

C++ in-order traversal, and please do not rely on buggy INT_MAX, INT_MIN solutions any more