/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode* root) {
return isValidBST(root, NULL, NULL);
}
bool isValidBST(TreeNode* root, TreeNode* _min, TreeNode* _max) {
if (root == NULL) return true;
if (_min != NULL && root->val <= _min->val ||
_max != NULL && root->val >= _max->val)
return false;
return isValidBST(root->left, _min, root) &&
isValidBST(root->right, root, _max);
}
};