private NeighborNode Clone(Dictionary <int, NeighborNode> dict, NeighborNode node) { if (node == null) { return(null); } if (dict.ContainsKey(node.val)) { return(dict[node.val]); } NeighborNode addNode = new NeighborNode(node.val, new List <NeighborNode>()); dict.Add(addNode.val, addNode); foreach (NeighborNode connectedNode in node.neighbors) { addNode.neighbors.Add(Clone(dict, connectedNode)); } return(addNode); }
public NeighborNode CloneGraph(NeighborNode node) { Dictionary <int, NeighborNode> dict = new Dictionary <int, NeighborNode>(); return(Clone(dict, node)); }