You are given an undirected graph that started as a tree with one extra edge added. Your task is to return the redundant edge whose removal makes the graph a tree again.
The redundant edge is the edge that creates a cycle in the graph.
Return the edge whose removal restores the tree structure.
Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]
Explanation: Removing edge [2,3] restores the tree.
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]
Explanation: Removing edge [1,4] removes the cycle.
Input: edges = [[1,2],[2,3],[3,1]]
Output: [3,1]
Explanation: The last edge completes the cycle.
Approach, complexity and clean code in every language. Sign in to unlock the editorial.
Your submissions, results and runtime history live here. Sign in to start your streak.
3 3 1 2 1 3 2 3
2 3
4 4 1 2 2 3 3 4 1 4
1 4
5 5 1 2 2 3 3 4 4 5 2 5
2 5