A reason you get run time error for {-1}

  • 0

    Firstly, {-1} means a graph with only one node, labeled -1 and no neighbors.

    I used a pointer to point to my newly created first node, and return that point after I cloned whole graph, then I get run time error for {-1} case. Apparently the test code check if you use reference more strictly than it should be.

    Here's my code that got run time error, if you change last line return head to return map[node] it will be accepted.

            if(!node) return node;
            queue<UndirectedGraphNode *> q;
            unordered_map<UndirectedGraphNode*, UndirectedGraphNode*> map;
            UndirectedGraphNode* head;
                UndirectedGraphNode * old = q.front();
                vector<UndirectedGraphNode *> oldNeighbors = old->neighbors;
                vector<UndirectedGraphNode *> newNeighbors;
                UndirectedGraphNode* newNode;
                    newNode = new UndirectedGraphNode(old->label);
                    map[old] = newNode;
                    if(!head) head = newNode;
                    newNode = map[old];
                for(UndirectedGraphNode* oldNei: oldNeighbors){
                        UndirectedGraphNode* newNei;
                            newNei = new UndirectedGraphNode(oldNei->label);
                            map[oldNei] = newNei;
                            newNei = map[oldNei];
                newNode->neighbors = newNeighbors;
            return head;

Log in to reply

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