Problem Statement
Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a value (int
) and a list (List[Node]
) of its neighbors.
Example 1:
Input:
12
 
43
Expected Output:
12
 
43
Explanation: The graph has four nodes with the following connections:
 Node
1
is connected to nodes2
and4
.  Node
2
is connected to nodes1
and3
.  Node
3
is connected to nodes2
and4
.  Node
4
is connected to nodes1
and3
.
Example 2:
Input:
12
/ \
5 3

4
Expected Output:
12
/ \
5 3

4
Explanation: The graph consists of five nodes with these connections:
 Node
1
is connected to nodes2
and5
.  Node
2
is connected to nodes1
and3
.  Node
3
is connected to nodes2
and4
.  Node
4
is connected to node3
.  Node
5
is connected to node1
.
Example 3:
Input:
12
/ \
4 3
\ /
56
Expected Output:
12
/ \
4 3
\ /
56
Explanation: The graph has six nodes with the following connections:
 Node
1
is connected to nodes2
and4
.  Node
2
is connected to nodes1
and3
.  Node
3
is connected to nodes2
and6
.  Node
4
is connected to nodes1
and5
.  Node
5
is connected to nodes4
and6
.  Node
6
is connected to nodes3
and5
.
Constraints:
 The number of nodes in the graph is in the range
[0, 100]
. 1 <= Node.val <= 100
Node.val
is unique for each node. There are no repeated edges and no selfloops in the graph.
 The Graph is connected and all nodes can be visited starting from the given node.
Try it yourself
Try solving this question here:
Python3
Python3