``````/**
* 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) {
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);
}
};``````

2021.03.07更新记录：

1、如果当前数据类型已是最小范围，判断会存在问题,例如树只有一个结点，值是TYPE_MIN或TYPE_MAX
2、上面例子或许存在跨平台问题，在32位系统中运行可能存在异常，long long只有在64位系统中才战8个字节，在32位系统中还是占用4个字节，因此和int范围相同，存在第1点所说的问题。

``````/**
* 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) {
if(root == nullptr) return true;
return isValidBST(root,LONG_MIN,LONG_MAX);
}
private:
bool isValidBST(TreeNode* root,long long left_bound,long long right_bound){
if(root == nullptr) return true;
if(root->val <= left_bound || root->val >= right_bound )
return false;
return isValidBST(root->left,left_bound,root->val) && isValidBST(root->right,root->val,right_bound);
}
};``````

``````/**
* 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.``````

2021.03.07更新代码:

``````class Solution {
public:
bool isValidBST(TreeNode* root) {
if(root == nullptr) return true;
return isValidBST(root,nullptr,nullptr);
}
private:
bool isValidBST(TreeNode* root,TreeNode* left_bound,TreeNode* right_bound){
if(root == nullptr) return true;
if(left_bound && root->val <= left_bound->val ||
right_bound && root->val >= right_bound->val)
return false;
return isValidBST(root->left,left_bound,root) && isValidBST(root->right,root,right_bound);
}
};``````

``````Runtime: 16 ms, faster than 48.77% of C++ online submissions for Validate Binary Search Tree.
Memory Usage: 21.5 MB, less than 85.86% 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 = nullptr;
return visited(root,prev);
}
private:
bool visited(TreeNode* root,TreeNode* &prev) {
if(root == nullptr) return true;
if(!visited(root->left,prev))
return false;
if(prev && prev->val >= root->val)
return false;
prev = root;
return visited(root->right,prev);
}
};``````

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