Exemplo n.º 1
0
 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);
 }
Exemplo n.º 2
0
 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;
 }
Exemplo n.º 4
0
 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);
 }
Exemplo n.º 5
0
 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;
 }
Exemplo n.º 6
0
 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;
 }
Exemplo n.º 7
0
 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 );
 }
Exemplo n.º 8
0
 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);
 }
Exemplo n.º 9
0
 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);
 }
Exemplo n.º 12
0
 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);
 }
Exemplo n.º 13
0
 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);
 }