Why my code is wrong for {-1,1#1}?


  • 1
    H
    private static Map<Integer, UndirectedGraphNode> newNodes = new HashMap<Integer, UndirectedGraphNode>();
    
    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
        if (node == null)
            return null;
    
        if (newNodes.containsKey(node.label))
            return newNodes.get(node.label);
        
        UndirectedGraphNode newNode = new UndirectedGraphNode(node.label);
        newNodes.put(node.label, newNode);
        
        for (UndirectedGraphNode nb : node.neighbors){
            UndirectedGraphNode newNB = cloneGraph(nb);
            newNode.neighbors.add(newNB);
        }
        return newNode;
    }

Log in to reply
 

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