// get all nodes in original graph
        public static LinkedList <CloningServiceTest.Node> GraphCloneHelper(CloningServiceTest.Node seed)
        {
            HashSet <CloningServiceTest.Node> vis   = new HashSet <CloningServiceTest.Node>();
            Queue <CloningServiceTest.Node>   queue = new Queue <CloningServiceTest.Node>();

            queue.Enqueue(seed);
            vis.Add(seed);

            while (queue.Count != 0)
            {
                CloningServiceTest.Node node = queue.Dequeue();
                if (!vis.Contains(node.Left))
                {
                    vis.Add(node.Left);
                    queue.Enqueue(node.Left);
                }

                if (!vis.Contains(node.Right))
                {
                    vis.Add(node.Right);
                    queue.Enqueue(node.Right);
                }
            }

            return(new LinkedList <CloningServiceTest.Node>(vis));
        }
        public static object GraphClone(CloningServiceTest.Node seed)
        {
            if (seed == null)
            {
                return(null);
            }
            LinkedList <CloningServiceTest.Node> nodes = GraphCloneHelper(seed);
            Dictionary <CloningServiceTest.Node, CloningServiceTest.Node> map = new Dictionary <CloningServiceTest.Node, CloningServiceTest.Node>();

            foreach (CloningServiceTest.Node node in nodes)
            {
                map.Add(node, new CloningServiceTest.Node());
            }

            foreach (CloningServiceTest.Node node in nodes)
            {
                CloningServiceTest.Node copied = map[node];
                if (node.Left != null)
                {
                    copied.Left = map[node.Left];
                }

                if (node.Right != null)
                {
                    copied.Right = map[node.Right];
                }
            }

            return(map[seed]);
        }
 private bool CheckSameNode(CloningServiceTest.Node oldNode, CloningServiceTest.Node currentClonedNode)
 {
     if (oldNode == null && currentClonedNode == null)
     {
         return(true);
     }
     if (oldNode == null || currentClonedNode == null)
     {
         return(false);
     }
     if (oldNode.Value == currentClonedNode.Value && oldNode.TotalNodeCount == currentClonedNode.TotalNodeCount)
     {
         return(CheckSameNode(oldNode.Left, currentClonedNode.Left) &&
                CheckSameNode(oldNode.Right, currentClonedNode.Right));
     }
     return(false);
 }
        private CloningServiceTest.Node[] CloneNodeCollection(CloningServiceTest.Node[] source)
        {
            var enumerable = source as IEnumerable <CloningServiceTest.Node>;

            if (enumerable != null)
            {
                var enumerList = enumerable.ToList();
                var dictionary = new Dictionary <int, List <CloningServiceTest.Node> >();
                var resultArr  = new CloningServiceTest.Node[enumerList.Count];
                for (var i = 0; i < enumerList.Count; i++)
                {
                    var currentClonedNode = Clone(enumerList[i]);
                    if (dictionary.Count == 0 || !dictionary.ContainsKey(currentClonedNode.TotalNodeCount))
                    {
                        resultArr[i] = currentClonedNode;
                        var newList = new List <CloningServiceTest.Node>();
                        newList.Add(currentClonedNode);
                        dictionary.Add(currentClonedNode.TotalNodeCount, newList);
                    }
                    else if (dictionary.ContainsKey(currentClonedNode.TotalNodeCount))
                    {
                        var nodeSet = dictionary[currentClonedNode.TotalNodeCount];
                        foreach (var node in nodeSet)
                        {
                            if (CheckSameNode(node, currentClonedNode))
                            {
                                resultArr[i] = node;
                            }
                            else
                            {
                                nodeSet.Add(currentClonedNode);
                                resultArr[i] = currentClonedNode;
                                dictionary[currentClonedNode.TotalNodeCount] = nodeSet;
                            }
                        }
                    }
                }
                return(resultArr);
            }
            return(new CloningServiceTest.Node[0]);
        }