98. Validate Binary Search Tree
class Solution {
public:
TreeNode *lastnode;
bool isValidBST(TreeNode *root) {
// write your code here
//按照中序遍历,应该为升序排列,若不满足,则不是BTS
if(root == NULL){
return true;
}
if(!isValidBST(root->left)){
return false;
}
if(lastnode!=NULL && lastnode->val>=root->val){
return false;
}
lastnode = root;
return isValidBST(root->right);
}
};