본문 바로가기

LeetCode - Top Interview 150

[Java Script] 133. Clone Graph

1. 문제 개요

문제 링크 : 133. Clone Graph

 

LeetCode - The World's Leading Online Programming Learning Platform

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

  • 그래프 기능 구현

2. 문제 풀이

 

그래프 기능을을 구현하면되는 간단한 문제입니다. 그래프에대한 이해도가 중요합니다.

 

LeetCode 요구 양식으로 코드 구현

var cloneGraph = function (node, clonedNodes = new Map()) {
  if (!node) return null;

  if (clonedNodes.has(node)) return clonedNodes.get(node);

  const clonedNode = new Node(node.val);

  clonedNodes.set(node, clonedNode);

  for (const neighbor of node.neighbors) {
    const clonedNeighbor = cloneGraph(neighbor, clonedNodes);

    clonedNode.neighbors.push(clonedNeighbor);
  }

  return clonedNode;
};

 

LeetCode 제출 결과

이렇게 구현에 성공하였습니다.

3. 시간 복잡도 계산

더보기

O(nlogn) = O(nlogn)

4. 깃허브

관련 사항은 깃허브에 모두 기재되어 있습니다.