6-line Concise JAVA Solution


  • 1
    M
    public class Solution {
        Map<Integer, UndirectedGraphNode> tmp = new HashMap<>();
        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
            if(node==null) return null;
            if(tmp.containsKey(node.label)) return tmp.get(node.label);
            UndirectedGraphNode nnode = new UndirectedGraphNode(node.label);
            tmp.put(nnode.label, nnode);
            for(UndirectedGraphNode ele:node.neighbors) nnode.neighbors.add(cloneGraph(ele));
            return nnode;
        }
    }
    
    

  • 0
    P

    Not readable.
    Don't write this kind of messy code in an Interview.


Log in to reply
 

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