コード例 #1
0
        private void Dfs(UndirectedGraphNode node, Dictionary <UndirectedGraphNode, UndirectedGraphNode> visited)
        {
            if (visited.ContainsKey(node))
            {
                return;
            }

            var newNode = new UndirectedGraphNode(node.label);

            foreach (var neighber in node.neighbors)
            {
                newNode.neighbors.Add(neighber);
            }

            visited.Add(node, newNode);
            foreach (var neignber in node.neighbors)
            {
                Dfs(neignber, visited);
            }
        }
コード例 #2
0
        public UndirectedGraphNode CloneGraph(UndirectedGraphNode node)
        {
            if (node == null)
            {
                return(null);
            }
            var visited = new Dictionary <UndirectedGraphNode, UndirectedGraphNode>();

            Dfs(node, visited);

            foreach (var newNode in visited.Values)
            {
                for (var i = 0; i < newNode.neighbors.Count; i++)
                {
                    newNode.neighbors[i] = visited[newNode.neighbors[i]];
                }
            }

            return(visited[node]);
        }