Can anyone tell me why the solution RE??


  • 0
    S

    I use Morris traversal, but I have no idea why i got RE on OJ but right result in local machine, could anyone help me? thanks.

    class Solution {
        int last;
        bool first = true;
        bool isValid(TreeNode *pNode) {
            if (first) {
                last = pNode->val;
                first = false;
                return true;
            }
            if (last > pNode->val)
                return false;
            last = pNode->val;
            return true;
        }
    public:
        bool isValidBST(TreeNode *root) {
            TreeNode *curr = root;
            while (curr) {
                if (!curr->left) {
                    if (!isValid(curr))
                        return false;
                    curr = curr->right;
                    continue;
                }
                TreeNode *pre = curr->left;
                while (pre->right && pre->right != curr)
                    pre = pre->right;
                if (!pre->right) {
                    pre->right = curr;
                    curr = curr->left;
                } else {
                    if (!isValid(curr))
                        return false;
                    pre->right = nullptr;
                    curr = curr->right;
                }
            } 
            return true;
        }
    };

  • 0
    A

    I have the same problem as yours. And I have test the error test case on my PC, the compiler is clang. And It's OK.


Log in to reply
 

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