this problem helps me to understand java better


  • 0
    F
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
            return cloneGraph(node, new HashMap<>());
        }
    
        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node, Map<Integer, UndirectedGraphNode> map) {
            if (node == null) {
                return null;
            }
            int label = node.label;
            if (map.containsKey(label)) {
                return map.get(label);
            }
            UndirectedGraphNode result = new UndirectedGraphNode(label);
            map.put(label, result);
            for (UndirectedGraphNode neighbor : node.neighbors) {
                result.neighbors.add(cloneGraph(neighbor, map));
            }
            return result;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.