Try to use Morris travesal but get RTE. The code runs correctly on my own laptop.


  • 1
    A
    class Solution {
    public:
        bool isValidBST(TreeNode* root) {
            TreeNode* cur = root;
            while (cur) {
                if (cur->right && cur->val >= cur->right->val) {
                    return false;
                }
    
                if (!cur->left) {
                    cur = cur->right;
                }
                else {
                    TreeNode * prev = cur->left;
                    while (prev->right && prev->right != cur) {
                        prev = prev->right;
                    }
                    if (prev->right) {
                        prev->right = nullptr;
                        cur = cur->right;
                    } else {
                        prev->right = cur;
                        cur = cur->left;
                    }
                }
            }
            return true;
        }
    };
    

    The following part checks if BST is valid by comparing each element to its next element (if there is one).

                if (cur->right && cur->val >= cur->right->val) {
                    return false;
                }
    

    Everything else is just the standard Morris travesal. I don't understand why I am getting RTE.


  • 0
    M

    will you explain more about "it is wrong for other reasons"? thanks!


  • 0
    Y

    Same problem here!!!!!!!


Log in to reply
 

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