Simplest 18ms C++


  • 1
    Y
    class Solution {
    public:
        bool isValidBST(TreeNode *root) {
            return isValid(root, nullptr, nullptr);
        }
        
        bool isValid(TreeNode* node, const int* allowedMin, const int* allowedMax) {
            if (!node) return true;
    
            if (allowedMin && node->val <= *allowedMin || allowedMax && node->val >= *allowedMax) {
                return false;
            }
            return isValid(node->left, allowedMin, &node->val)
                && isValid(node->right, &node->val, allowedMax);
        }
    };

Log in to reply
 

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