JavaScript Solution


  • 1
    L
    function cloneGraph(graph) {
      var map = {};
      return traverse(graph);
    
      function traverse(node) {
        if (!node) return node;
        if (!map[node.label]) {
          map[node.label] = new UndirectedGraphNode(node.label);
          map[node.label].neighbors = node.neighbors.map(traverse);
        }
        return map[node.label];
      }
    }

Log in to reply
 

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