My code end with Time Limit Exceeded. Please help


  • 0
    D
    public class Solution {
     public  UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
    	 // if(node==null)return null;
    	  UndirectedGraphNode resultNode=null;//new UndirectedGraphNode(node.label);
    	  
    	  resultNode=cloneGraph(node,resultNode);
    	  return resultNode;      
    	 }
    	 
    	 public  UndirectedGraphNode cloneGraph(UndirectedGraphNode node,UndirectedGraphNode resultNode)
    	 {
    	     if(node==null)return null;
    		 resultNode=new UndirectedGraphNode(node.label);
    		 UndirectedGraphNode tmp=null;
    		 for(UndirectedGraphNode thisNode:node.neighbors)
    		 {
    			 if(thisNode.equals(node))resultNode.neighbors.add(node);
    			 else resultNode.neighbors.add(cloneGraph(thisNode,tmp));
    		 }
    		 
    		 return resultNode;
    	}
    

    }


  • 1
    R

    You are not telling visited nodes and unvisited nodes.


Log in to reply
 

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