StackoverFlow Error at line 34 for input {0,0,0} please help.


  • -1
    V

    public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {

        if(null == node)
        
             return null;
             
        Map<UndirectedGraphNode,UndirectedGraphNode>  map = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
        
        return cloneHelper(node,map);
        
        
        
    }
    
    private UndirectedGraphNode cloneHelper(UndirectedGraphNode node,Map<UndirectedGraphNode,UndirectedGraphNode> map ){
        
       if(null == node)
       
         return node;
         
        UndirectedGraphNode  clonedNode = null;
         
        if(map.get(node) == null){
            
          clonedNode = new UndirectedGraphNode(node.label);
          map.put(node,clonedNode);
            
        }
        
        else{
          clonedNode = map.get(node);  
        }
        
        List<UndirectedGraphNode>  kids = node.neighbors;
        
        
        for(UndirectedGraphNode nd:kids){
            
            clonedNode.neighbors.add(cloneHelper(nd,map));
           
        }
        
        
        return clonedNode;
        
        
    }

Log in to reply
 

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