g0101_0200.s0133_clone_graph.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
package g0101_0200.s0133_clone_graph;
// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search #Graph
import com_github_leetcode.Node;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class Solution {
public Node cloneGraph(Node node) {
return cloneGraph(node, new HashMap<>());
}
private Node cloneGraph(Node node, Map processedNodes) {
if (node == null) {
return null;
} else if (processedNodes.get(node) != null) {
return processedNodes.get(node);
}
Node newNode = new Node();
processedNodes.put(node, newNode);
newNode.val = node.val;
newNode.neighbors = new ArrayList<>();
for (Node neighbor : node.neighbors) {
Node clonedNeighbor = cloneGraph(neighbor, processedNodes);
if (clonedNeighbor != null) {
newNode.neighbors.add(clonedNeighbor);
}
}
return newNode;
}
}