bool isValidBSTHelper(TreeNode *root, int low, int high){ if (!root) return true; if (root->val <= low || root->val >= high) return false; return isValidBSTHelper(root->left, low, root->val) && isValidBSTHelper(root->right, root->val, high); }
bool isValidBSTHelper( TreeNode *root, int min, int max ) { if( root == NULL ) return true; return (root->val>min&&root->val<max) &&isValidBSTHelper( root->left, min, root->val ) &&isValidBSTHelper( root->right, root->val, max ); }
bool isValidBSTHelper(TreeNode *node, TreeNode* &min) { if (!node) return true; if (node->left && !isValidBSTHelper(node->left, min)) return false; if (min && node->val <= min->val) return false; min = node; if (node->right && !isValidBSTHelper(node->right, min)) return false; return true; }
bool isValidBSTHelper(TreeNode* root, long min, long max){ if(!root){ return true; } if((root->val >= max) || (root->val <= min)){ return false; } return isValidBSTHelper(root->left, min, root->val) && isValidBSTHelper(root->right, root->val, max); }
bool isValidBSTHelper(TreeNode* node, int &prev) { if (node == NULL) return true; if (isValidBSTHelper(node->left, prev)) { if (prev < node->val) { prev = node->val; return isValidBSTHelper(node->right, prev); } return false; } return false; }
bool isValidBSTHelper(TreeNode* root, int min, int max) { if(root == NULL) { return true; } if(root->val <= min || root->val >= max) { // 这里是否取等号可以和下面的isValidBSTHelper的第一个参数结合起来考虑 return false; } bool left = isValidBSTHelper(root->left, min, root->val); bool right = isValidBSTHelper(root->right, root->val, max); return left && right; }
bool isValidBST(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function if( root == NULL ) return true; int min = -100000; int max = 100000; return isValidBSTHelper( root, min, max ); }
bool isValidBST(TreeNode *root) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. return isValidBSTHelper(root, INT_MIN, INT_MAX); }
bool isValidBST(TreeNode* root) { return isValidBSTHelper(root, INT_MIN - 1L, INT_MAX + 1L); }
// Inorder traversal (lower bound) bool isValidBST(TreeNode *root) { TreeNode* min = NULL; return isValidBSTHelper(root, min); }
bool isValidBSTHelper(TreeNode *node, TreeNode* min, TreeNode* max) { if (!node) return true; if ((min && node->val <= min->val) || (max && node->val >= max->val)) return false; return isValidBSTHelper(node->left, min, node) && isValidBSTHelper(node->right, node, max); }
bool isValidBST(TreeNode *root) { // Start typing your C/C++ solution below // DO NOT write int main() function int prev = INT_MIN; return isValidBSTHelper(root, prev); }
bool isValidBST(TreeNode *root) { // Note: The Solution object is instantiated only once and is reused by each test case. return isValidBSTHelper(root,INT_MIN,INT_MAX); }