cheetcode

89. Redundant ConnectionMedium

Find the edge that creates a cycle in an otherwise tree-like graph.

You are given edges that form a tree plus one extra edge.

Return the extra edge that can be removed to restore a tree.

Model the relationships explicitly, then choose BFS, DFS, or union find based on the question. Track visited state so cycles do not create repeated work.

Examples

Input: edges = [[1,2],[1,3],[2,3]]
Output: [2,3]
Input: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
Output: [1,4]

Constraints

  • n == edges.length
  • 3 <= n <= 1000