https://leetcode.com/problems/clone-graph/?envType=study-plan-v2&envId=top-interview-150
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
class Solution {
public Node cloneGraph(Node node) {
if(node == null) return null;
Map<Node, Node> map = new HashMap<>();
Queue<Node> q = new LinkedList<>();
map.put(node, new Node(node.val));
q.add(node);
while(!q.isEmpty()) {
Node currNode = q.poll();
Node cloneNode = map.get(currNode);
for(Node adjNode: currNode.neighbors) {
if(!map.containsKey(adjNode)) {
map.put(adjNode, new Node(adjNode.val));
q.add(adjNode);
}
cloneNode.neighbors.add(map.get(adjNode));
}
}
return map.get(node);
}
}
/*
// Definition for a Node.
class Node {
public int val;
public List<Node> neighbors;
public Node() {
val = 0;
neighbors = new ArrayList<Node>();
}
public Node(int _val) {
val = _val;
neighbors = new ArrayList<Node>();
}
public Node(int _val, ArrayList<Node> _neighbors) {
val = _val;
neighbors = _neighbors;
}
}
*/
class Solution {
public Node cloneGraph(Node node) {
if(node == null) return null;
Node visited[] = new Node[101];
Node copiedNode = new Node(node.val);
Queue<Node> q = new LinkedList<>();
q.add(node);
visited[copiedNode.val] = copiedNode;
while(!q.isEmpty()){
Node popOrig = q.poll();
for(Node n:popOrig.neighbors){
if(visited[n.val] == null){
visited[n.val] = new Node(n.val);
q.add(n);
}
visited[popOrig.val].neighbors.add(visited[n.val]);
}
}
return copiedNode;
}
}