Redundant Connection
You start with a tree of n nodes labeled 1..n (n-1 edges, connected, acyclic), then one extra edge is added, creating exactly one cycle. Given the list of edges in the order they were added, return the one edge that can be removed so the graph is a tree again. If several answers exist, return the edge that appears last in the input.
Open official problem prompt ↗Find the single edge whose removal turns the given one-cycle graph back into a spanning tree, preferring the later edge when there is a tie.
Imagine merging friend groups: each edge says two people are friends, so you merge their groups. The edge that connects two people already in the same group tells you nothing new; that is the redundant introduction.
- Input
- edges = [[1,2],[1,3],[2,3]]
- Output
- [2,3]
- Why
- Nodes 1, 2, 3 are already connected by [1,2] and [1,3]; adding [2,3] closes a cycle, so [2,3] is the redundant edge.
n == edges.length3 <= n <= 1000edges[i].length == 21 <= ai < bi <= edges.lengthai != biThere are no repeated edgesThe given graph is connected and has exactly one cycle