Could some one spot the Bug ? - I am getting {0,0,0#0#0} as an output for {0,0,0}


  • 0
    M
    class Solution {
    public:
        UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
            unordered_map<UndirectedGraphNode *, int> visited;
            if(!node)
                return NULL;
            UndirectedGraphNode *clone = new UndirectedGraphNode(node->label);
            
            return recurse(node, visited, clone);
        }
        UndirectedGraphNode *recurse(UndirectedGraphNode *node, unordered_map<UndirectedGraphNode *, int> &visited, UndirectedGraphNode* &clone)
        {
            if(!node)
                return NULL;
    
            if(visited[node])
            {
                return NULL;
            }
            for(int i = 0; i < node->neighbors.size(); i++)
            {
                clone->neighbors.push_back(new UndirectedGraphNode(node->neighbors[i]->label));
            }
            
            visited[node] = 1;
            for(int i = 0; i < node->neighbors.size(); i++)
            {
                 recurse(node->neighbors[i], visited, clone->neighbors[i]);
            }
            return clone;
        }
        
    };

Log in to reply
 

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