Can't firure this out: runtime error/; last executed input [1,1]


  • 0
    1
    /**
     * Definition for a binary tree node.
     * struct TreeNode {
     *     int val;
     *     TreeNode *left;
     *     TreeNode *right;
     *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
     * };
     */
    class Solution {
    public:
        bool isValidBST(TreeNode *root) 
        {
            TreeNode *bridge, *cur, *prev;
            cur = root;
            while (cur)
            {
                if (cur->left)
                {
                    bridge = cur->left;
    				while (bridge->right && bridge->right != cur)
    				{
    					bridge = bridge->right;
    				}
                    if (!bridge->right)
                    {
                        bridge->right = cur;
                        cur = cur->left;
                    }
                    else
                    {
                        bridge->right = NULL;
                        if (!cur->right)
                            return true;
                        prev = cur;
                        cur = cur->right;
                        if (prev->val >= cur->val)
                            return false;
                    }
                }
                else
                {
    				if (!cur->right)
                        return true;
                    prev = cur;
                    cur = cur->right;
                    if (prev->val >= cur->val)
                        return false;
                }
            }
            return true;
        }
    };

  • 0
    1

    it actually works on my VS but gets wrong in here :((


  • 2
    W

    Hi.

    Your code return the result right after you detect an error, leaving the tree structure changed.


  • 0
    1

    I didn't notice, thanks :)


Log in to reply
 

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