```
class Solution {
public:
bool isValidBST(TreeNode* root) {
if (root == NULL) {
return true;
}
TreeNode * cur = root;
int lastVal = INT_MIN;
while (cur != NULL) {
if (cur->left == NULL) {
if (cur->val <= lastVal) {
return false;
}
lastVal = cur->val;
cur = cur->right;
} else {
TreeNode * temp = cur->left;
while (temp->right != NULL && temp->right != cur) {
temp = temp->right;
}
if (temp->right == NULL) {
temp->right = cur;
cur = cur->left;
} else {
if (cur->val <= lastVal) {
return false;
}
lastVal = cur->val;
temp->right = NULL;
cur = cur->right;
}
}
}
return true;
}
};
```