What is wrong with this code?


  • 0
    L
    class Solution {
    public:
        map<int,UndirectedGraphNode*> mp;
        UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
            if(node==NULL)
                return NULL;
            cout<<"map size: "<<mp.size()<<endl;
            if(mp.count(node->label)>0)
                return mp[node->label];
            else
            {
                UndirectedGraphNode *cloneNode = new UndirectedGraphNode(node->label);
                cout<<"Cloning: "<<node->label<<endl;
                vector<UndirectedGraphNode*> cloneNeighbors = cloneNode->neighbors;
                vector<UndirectedGraphNode*> neighbors = node->neighbors;
                mp[node->label] = cloneNode;
                for(int i=0;i<neighbors.size();i++)
                    cloneNeighbors.push_back(cloneGraph(neighbors[i]));
                cout<<"neighbours size: "<<cloneNeighbors.size()<<endl;
                return cloneNode;
            }
        }
    };
    

  • 0
    L

    It fails for the case {0,0,0}


  • 0
    S

    neighbors list could contain duplicate vertexes.

    {0, 0, 0}: Node(0).neightbors = [0,0]


Log in to reply
 

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