判断是否为二叉搜索树
定义上下确界
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isValidBST(TreeNode *root) {
return isValidBSTRe(root,LONG_MIN,LONG_MAX);//LONG_MIN,LONG_MAX set the bound
}
bool isValidBSTRe(TreeNode *root,long min,long max){//parameter type long
if(!root)
return true;
else if(root->val<=min||root->val>=max)
return false;
else
return isValidBSTRe(root->left,min,root->val)&&isValidBSTRe(root->right,root->val,max);
}
};