思路:递归求解的时候需要传入区间,即当前节点的值必须落入的范围。
code:
class Solution {
public:
bool isBST(TreeNode *root, int leftBoundary, int rightBoundary){
if(root != NULL){
if(root->val > leftBoundary && root->val < rightBoundary){
return isBST(root->left,leftBoundary,root->val) && isBST(root->right,root->val,rightBoundary);
}
return false;
}
return true;
}
bool isValidBST(TreeNode *root) {
return isBST(root,INT_MIN,INT_MAX);
}
};