Instead of passing values for max and min, pass TreeNode for min and max (will solve INT_MAX problem)


  • 0
    G

    bool isValidBSTUtil(TreeNode *root, TreeNode *minNode, TreeNode *maxNode) {
    if (root == NULL)
    return true;

        if ((maxNode != NULL) && root->val >= maxNode->val)
            return false;
        if ((minNode != NULL) && root->val <= minNode->val)
            return false;
        
        if (isValidBSTUtil(root->right, root, maxNode) && isValidBSTUtil(root->left, minNode, root))
            return true;
        else 
            return false;
    

    }

    bool isValidBST(TreeNode* root) {
        TreeNode *minNode = NULL;
        TreeNode *maxNode = NULL;
        return isValidBSTUtil(root, minNode, maxNode);    
    }

Log in to reply
 

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