How to fix the corner case, I failed in input only having one variable {INT_MAX}

  • 1
    bool isValidBST(TreeNode *root) 
        return judge(root, INT_MIN, INT_MAX);
    bool judge(TreeNode *root, int MIN, int MAX)
        if (root == NULL) return true;
        if (root->val <= MIN || root->val >= MAX) return false;
        return judge(root->left, MIN, root->val) && judge(root->right, root->val, MAX); 

  • 2

    You can try LONG_MAX and LONG_MIN, I am not sure this is the best solustion, it just pass the test cases.

  • 0

    I think another way is to have two boolean variables, say visitedMinInteger, visitiedMaxInteger, which will turn to true if min or max integer value is first met. For only the node with value min or max integer, the special condition will be checked if any other has already been visited using the same value by checking the boolean variables. Otherwise, the usual code will run. This will make sure that no conversion of integer to long is required.

Log in to reply

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