Solution 1 : Preorder traversal.

```
class Solution {
public:
bool isValidBST(TreeNode *root) {
if(root == NULL) return true;
TreeNode *pre = NULL, *post = NULL;
if(root->left) {
pre = root->left;
while(pre->right) pre = pre->right;
if(pre->val >= root->val) return false;
}
if(root->right) {
post = root->right;
while(post->left) post = post->left;
if(post->val <= root->val) return false;
}
return isValidBST(root->left) && isValidBST(root->right);
}
};
```

Solution 2: Inorder traversal.

```
bool isBST(TreeNode *root, int& preV, bool &first) {
if(root == NULL) return true;
bool l = isBST(root->left, preV, first);
if(first) first = false;
else if(preV >= root->val) return false;
preV = root->val;
bool r = isBST(root->right, preV, first);
return l && r;
}
class Solution {
public:
bool isValidBST(TreeNode *root) {
int preV;
bool first = true;
return isBST(root, preV, first);
}
};
```

Solution 3: Morris Traversal.

```
class Solution {
public:
bool isValidBST(TreeNode *root) {
bool result = true;
TreeNode *cur = root, *pre, *node1, *node2;
node1 = node2 = NULL;
while(cur) {
if(cur->left == NULL) {
if(node1 == NULL) node1 = cur;
else if(node2 == NULL) node2 = cur;
else { node1 = node2; node2 = cur; }
cur = cur->right;
} else {
pre = cur->left;
while(pre->right && pre->right != cur) pre = pre->right;
if(pre->right == NULL) {
pre->right = cur;
cur = cur->left;
continue;
} else {
pre->right = NULL;
if(node2 == NULL) node2 = cur;
else {node1 = node2; node2 = cur; }
cur = cur->right;
}
}
if(node1 && node2 && node1->val >= node2->val)
result = false;
}
return result;
}
};
```

Any questions ? Ask me here is OK.