This is a divide and conquer find the minimum + maximum. Binary search tree root node than the left subtree maximum but smaller than a minimum right subtree.
class Solution {
private:
public:
bool isValidBST(TreeNode* root) {
if(root == NULL) return true;
TreeNode* max_left = root->left;
TreeNode* min_right = root->right;
while(max_left != NULL && max_left->right != NULL){
max_left = max_left->right;
}
while(min_right != NULL && min_right->left != NULL){
min_right = min_right->left;
}
bool result = true;
if(max_left) result = result && root->val > max_left->val;
if(min_right) result = result && root->val < min_right->val;
return isValidBST(root->left) && isValidBST(root->right) && result;
}
};