I use a "static TreeNode *" by mocking others' method, but it's failed. Can you take it care?


  • 0
    L
    bool isValidBST(TreeNode* root) {
         static TreeNode * prev = NULL;
         if (root == NULL) return true;
         if (!isValidBST(root->left)) return false;
         if (prev && root->val <= prev->val) {
             return false;
         }
         prev = root;
         return isValidBST(root->right);
    }

  • 0

    Please read item 3. from the FAQ. If you are using static or global variables, please remember to reset them between each test case.


  • 0
    L

    Thank you, your answer perfectly explain my doubts.


Log in to reply
 

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