/**
* 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 flag = false;
int lastVal = 0;
bool isValidBST(TreeNode* root) {
if (!root) {
return true;
}
if (!isValidBST(root->left)) {
return false;
}
if (!flag) {
flag = true;
} else {
if (root->val <= lastVal) {
return false;
}
}
lastVal = root->val;
if (!isValidBST(root->right)) {
return false;
}
return true;
}
};