Runtime error, no idea why.


  • 0
    A
    /**
     * 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:
        vector<int> inorderTraversal(TreeNode* root) {
            // threaded binary tree
            set<int> s;
            vector<int> rst;
    
            while(root != NULL) {
                s.insert(root->val);
                
                if (root->left != NULL && s.count(root->left->val) == 0) {
                    auto tmp = root->left;
                    while(tmp->right)
                        tmp = tmp->right;
                    tmp->right = root;
                    root = root->left;
                } else {
                    rst.push_back(root->val);
                    cout << root-> val << endl;
                    cout << root->right << endl;
                    root = root->right;
                }
            }
    
            cout << "FUCK" << endl;
            return rst;
        }
    };
    

    Want to use the threaded binary tree solution, but there is always a runtime error even on 'Run Code' test case. [1, null, 2, 3].

    I ran it on my local machine and everything goes well, print some words even just before the return statement is also ok, but keeps give out error.

    Does anybody have any idea bout it?

    Thank you.


  • 0
    C

    @aubee Same problem. Same code runs fine on another coding site. Did you find a solution?


  • 0
    A

    No idea, I rewrite the whole program with another approach and passed the question. I guess there must be something wrong with the judger. Don't waste time on it.


  • 0
    A

    @codeRiya Oh I guess the problem is that I modified the given data, TreeNode *. I got another probelm on another question and I found it's because I modified the given data and the judge can't reuse them.


Log in to reply
 

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