/**
* 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) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
if(!root)
return true;
int min=INT_MIN;
int max=INT_MAX;
return helper(root,min,max);
}
bool helper(TreeNode* node,int min, int max){
if(!node)
return true;
if(node->val<=min||node->val>=max)
return false;
return helper(node->left,min,node->val)&&helper(node->right,node->val,max);
}
};
Leetcode: isBST
最新推荐文章于 2019-06-25 17:24:43 发布