Advertisement
1988coder

133. Clone Graph

Jul 28th, 2019
422
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.36 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/clone-graph/
  3.  
  4. import java.util.ArrayList;
  5. import java.util.HashMap;
  6. import java.util.LinkedList;
  7. import java.util.List;
  8. import java.util.Map;
  9. import java.util.Queue;
  10.  
  11. // Definition for a Node.
  12. // class Node {
  13. //     public int val;
  14. //     public List<Node> neighbors;
  15.  
  16. //     public Node() {
  17. //     }
  18.  
  19. //     public Node(int _val, List<Node> _neighbors) {
  20. //         val = _val;
  21. //         neighbors = _neighbors;
  22. //     }
  23. // };
  24.  
  25. /**
  26.  * DFS - Recursive
  27.  *
  28.  * Time Complexity: O(V + E)
  29.  *
  30.  * Space Complexity: O(V).
  31.  *
  32.  * V = Number of nodes. E = Number of edges in the graph.
  33.  */
  34. class Solution {
  35.     public Node cloneGraph(Node node) {
  36.         if (node == null) {
  37.             return null;
  38.         }
  39.  
  40.         Map<Node, Node> map = new HashMap<>();
  41.  
  42.         return cloneGraphHelper(node, map);
  43.     }
  44.  
  45.     private Node cloneGraphHelper(Node node, Map<Node, Node> map) {
  46.         if (node == null) {
  47.             return null;
  48.         }
  49.         if (map.containsKey(node)) {
  50.             return map.get(node);
  51.         }
  52.  
  53.         Node copy = new Node(node.val, new ArrayList<>());
  54.         map.put(node, copy);
  55.  
  56.         for (Node n : node.neighbors) {
  57.             copy.neighbors.add(cloneGraphHelper(n, map));
  58.         }
  59.  
  60.         return copy;
  61.     }
  62. }
  63.  
  64. /**
  65.  * BFS - Iterative
  66.  *
  67.  * Time Complexity: O(V + E)
  68.  *
  69.  * Space Complexity: O(V).
  70.  *
  71.  * V = Number of nodes. E = Number of edges in the graph.
  72.  */
  73. class Solution2 {
  74.     public Node cloneGraph(Node node) {
  75.         if (node == null) {
  76.             return null;
  77.         }
  78.  
  79.         Map<Node, Node> map = new HashMap<>();
  80.         Queue<Node> queue = new LinkedList<>();
  81.  
  82.         Node clone = new Node(node.val, new ArrayList<>());
  83.  
  84.         map.put(node, clone);
  85.         queue.offer(node);
  86.  
  87.         while (!queue.isEmpty()) {
  88.             Node cur = queue.poll();
  89.             Node copy = map.get(cur);
  90.  
  91.             for (Node n : cur.neighbors) {
  92.                 if (map.containsKey(n)) {
  93.                     copy.neighbors.add(map.get(n));
  94.                 } else {
  95.                     Node nCopy = new Node(n.val, new ArrayList<>());
  96.                     map.put(n, nCopy);
  97.                     copy.neighbors.add(nCopy);
  98.                     queue.offer(n);
  99.                 }
  100.             }
  101.         }
  102.  
  103.         return clone;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement