You are given an undirected graph that started as a tree and then had one extra edge added. Return the edge that can be removed so the graph becomes a tree again. Pattern focus: Kruskal. Detect the first edge that connects two vertices already in the same component.
edges = list of undirected edges in a graph with exactly one extra edge
the edge that creates the cycle
Example 1:
Input:
edges = [[1,2],[1,3],[2,3]]
Output:
[2,3]
Explanation:
Edge [2,3] closes the only cycle.
Example 2:
Input:
edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output:
[1,4]
Explanation:
Removing [1,4] restores the tree structure.
Example 3:
Input:
edges = [[1,2],[2,3],[3,1],[3,4]]
Output:
[3,1]
Explanation:
The cycle is formed when edge [3,1] is added.