forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathClone Graph.java
52 lines (49 loc) · 2.39 KB
/
Clone Graph.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
// 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 void dfs(Node node , Node copy , Node[] visited){
visited[copy.val] = copy;// store the current node at it's val index which will tell us that this node is now visited
// now traverse for the adjacent nodes of root node
for(Node n : node.neighbors){
// check whether that node is visited or not
// if it is not visited, there must be null
if(visited[n.val] == null){
// so now if it not visited, create a new node
Node newNode = new Node(n.val);
// add this node as the neighbor of the prev copied node
copy.neighbors.add(newNode);
// make dfs call for this unvisited node to discover whether it's adjacent nodes are explored or not
dfs(n , newNode , visited);
}else{
// if that node is already visited, retrieve that node from visited array and add it as the adjacent node of prev copied node
// THIS IS THE POINT WHY WE USED NODE[] INSTEAD OF BOOLEAN[] ARRAY
copy.neighbors.add(visited[n.val]);
}
}
}
public Node cloneGraph(Node node) {
if(node == null) return null; // if the actual node is empty there is nothing to copy, so return null
Node copy = new Node(node.val); // create a new node , with same value as the root node(given node)
Node[] visited = new Node[101]; // in this question we will create an array of Node(not boolean) why ? , because i have to add all the adjacent nodes of particular vertex, whether it's visited or not, so in the Node[] initially null is stored, if that node is visited, we will store the respective node at the index, and can retrieve that easily.
Arrays.fill(visited , null); // initially store null at all places
dfs(node , copy , visited); // make a dfs call for traversing all the vertices of the root node
return copy; // in the end return the copy node
}
}