Skip to content
Veltoryx Technologies
ProProblem 126● Mediumleetcode ↗4 min readUpdated 30 October 2026

Redundant Connection

Detect the edge that creates a cycle in an undirected graph using Union-Find.

01The problem

A tree is an undirected graph that is connected and has no cycles.

You are given a graph that started as a tree with n nodes labeled from 1 to n, with one additional edge added. The added edge created a cycle. The edges are given as an array edges where edges[i] = [a, b] indicates an edge between nodes a and b.

Return an edge that can be removed so that the resulting graph is a tree of n nodes. If there are multiple answers, return the answer that occurs last in the input.

Loading the rest of the lesson…