If I make the map as private static, it fails, it seems a bug for leetcode


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

Log in to reply
 

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