C++ solution using LONG_MIN and LONG_MAX


  • 0
    S
    class Solution {
    public:
        bool isValidBSTRec(TreeNode* root, long min, long max) {
            if(!root) return true;
            if((root->val<max) && (root->val>min)) {
                return (isValidBSTRec(root->left, min, root->val)) &&
                        (isValidBSTRec(root->right, root->val, max));  
            }
            return false; 
        }
        bool isValidBST(TreeNode* root) {
            if(root && !root->left && !root->right) return true;
            return isValidBSTRec(root, LONG_MIN, LONG_MAX);
    
        }
    };
    

Log in to reply
 

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