|
| 1 | +import java.util.HashMap; |
| 2 | + |
| 3 | +/* |
| 4 | + * @lc app=leetcode id=133 lang=java |
| 5 | + * |
| 6 | + * [133] Clone Graph |
| 7 | + * |
| 8 | + * https://leetcode.com/problems/clone-graph/description/ |
| 9 | + * |
| 10 | + * algorithms |
| 11 | + * Medium (38.66%) |
| 12 | + * Total Accepted: 435.9K |
| 13 | + * Total Submissions: 1.1M |
| 14 | + * Testcase Example: '[[2,4],[1,3],[2,4],[1,3]]' |
| 15 | + * |
| 16 | + * Given a reference of a node in a connected undirected graph. |
| 17 | + * |
| 18 | + * Return a deep copy (clone) of the graph. |
| 19 | + * |
| 20 | + * Each node in the graph contains a val (int) and a list (List[Node]) of its |
| 21 | + * neighbors. |
| 22 | + * |
| 23 | + * |
| 24 | + * class Node { |
| 25 | + * public int val; |
| 26 | + * public List<Node> neighbors; |
| 27 | + * } |
| 28 | + * |
| 29 | + * |
| 30 | + * |
| 31 | + * |
| 32 | + * Test case format: |
| 33 | + * |
| 34 | + * For simplicity sake, each node's value is the same as the node's index |
| 35 | + * (1-indexed). For example, the first node with val = 1, the second node with |
| 36 | + * val = 2, and so on. The graph is represented in the test case using an |
| 37 | + * adjacency list. |
| 38 | + * |
| 39 | + * Adjacency list is a collection of unordered lists used to represent a finite |
| 40 | + * graph. Each list describes the set of neighbors of a node in the graph. |
| 41 | + * |
| 42 | + * The given node will always be the first node with val = 1. You must return |
| 43 | + * the copy of the given node as a reference to the cloned graph. |
| 44 | + * |
| 45 | + * |
| 46 | + * Example 1: |
| 47 | + * |
| 48 | + * |
| 49 | + * Input: adjList = [[2,4],[1,3],[2,4],[1,3]] |
| 50 | + * Output: [[2,4],[1,3],[2,4],[1,3]] |
| 51 | + * Explanation: There are 4 nodes in the graph. |
| 52 | + * 1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = |
| 53 | + * 4). |
| 54 | + * 2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = |
| 55 | + * 3). |
| 56 | + * 3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = |
| 57 | + * 4). |
| 58 | + * 4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = |
| 59 | + * 3). |
| 60 | + * |
| 61 | + * |
| 62 | + * Example 2: |
| 63 | + * |
| 64 | + * |
| 65 | + * Input: adjList = [[]] |
| 66 | + * Output: [[]] |
| 67 | + * Explanation: Note that the input contains one empty list. The graph consists |
| 68 | + * of only one node with val = 1 and it does not have any neighbors. |
| 69 | + * |
| 70 | + * |
| 71 | + * Example 3: |
| 72 | + * |
| 73 | + * |
| 74 | +* Input: adjList = [] |
| 75 | +* Output: [] |
| 76 | +* Explanation: This an empty graph, it does not have any nodes. |
| 77 | +* |
| 78 | +* |
| 79 | +* Example 4: |
| 80 | +* |
| 81 | +* |
| 82 | +* Input: adjList = [[2],[1]] |
| 83 | +* Output: [[2],[1]] |
| 84 | +* |
| 85 | +* |
| 86 | +* |
| 87 | +* Constraints: |
| 88 | +* |
| 89 | +* |
| 90 | +* 1 <= Node.val <= 100 |
| 91 | +* Node.val is unique for each node. |
| 92 | +* Number of Nodes will not exceed 100. |
| 93 | +* There is no repeated edges and no self-loops in the graph. |
| 94 | +* The Graph is connected and all nodes can be visited starting from the given |
| 95 | +* node. |
| 96 | +* |
| 97 | +* |
| 98 | +*/ |
| 99 | +/* |
| 100 | +// Definition for a Node. |
| 101 | +class Node { |
| 102 | +public int val; |
| 103 | +public List<Node> neighbors; |
| 104 | +public Node() { |
| 105 | +val = 0; |
| 106 | +neighbors = new ArrayList<Node>(); |
| 107 | +} |
| 108 | +public Node(int _val) { |
| 109 | +val = _val; |
| 110 | +neighbors = new ArrayList<Node>(); |
| 111 | +} |
| 112 | +public Node(int _val, ArrayList<Node> _neighbors) { |
| 113 | +val = _val; |
| 114 | +neighbors = _neighbors; |
| 115 | +} |
| 116 | +} |
| 117 | +*/ |
| 118 | + |
| 119 | +class Solution { |
| 120 | + public Node cloneGraph(Node node) { |
| 121 | + if (node == null) return null; |
| 122 | + |
| 123 | + HashMap<Integer, Node> visited = new HashMap<>(); |
| 124 | + return cloneGraph(node, visited); |
| 125 | + } |
| 126 | + |
| 127 | + public Node cloneGraph(Node root, HashMap<Integer, Node> visited) { |
| 128 | + if (visited.containsKey(root.val)) { |
| 129 | + return visited.get(root.val); |
| 130 | + } |
| 131 | + |
| 132 | + Node newNode = new Node(root.val); |
| 133 | + |
| 134 | + visited.put(newNode.val, newNode); |
| 135 | + |
| 136 | + for (Node neighbor : root.neighbors) { |
| 137 | + newNode.neighbors.add(cloneGraph(neighbor, visited)); |
| 138 | + } |
| 139 | + |
| 140 | + return newNode; |
| 141 | + } |
| 142 | +} |
0 commit comments