Short Python solution

  • 0

    def findRedundantConnection(self, edges):

        n = len(edges)
        graph = {}
        for i in range(1, n + 1):
            graph[i] = {i}
        for e in edges:
            if graph[e[0]] == graph[e[1]]:
                return e
            graph[e[0]] |= graph[e[1]]
            for node in graph[e[0]]:
                graph[node] = graph[e[0]]
        return None


Log in to reply

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