My 2 recursion cpp codes


  • 0
    C

    Recursion1:

    class Solution {
    public:
    
        bool helper(TreeNode *root, long long int min, long long int max)
        {
            if(!root) return true;
            
            if( !(min < root->val && root->val < max) )
                return false;
                
            return helper(root->left, min, root->val) && helper(root->right, root->val, max);
        }
    
        bool isValidBST(TreeNode* root) 
        {
            return helper(root, (unsigned long long int)1<<63, ~((unsigned long long int)1<<63));
        }
    };
    

    Recursion2:

    class Solution {
    public:
    
        TreeNode *node;
    
        bool helper(TreeNode *root)
        {
            if(!root) return true;
            
            if( !helper(root->left) ) return false;
            
            if(node && node->val >= root->val) return false;
            node = root;
            
            return helper(root->right);
        }
    
        bool isValidBST(TreeNode* root) 
        {
            node = NULL;
            return helper(root);
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.