LeetCode 684 Redundant Connection - Solution

The core idea

Process edges one by one with Union-Find. When you try to union two nodes that are already connected, you've found the redundant edge.

Initially, each node is its own component. For each edge [u, v]:

  • If find(u) == find(v), nodes are already connected. This edge creates a cycle. Return it.
  • Otherwise, union them.

The last edge that creates a cycle is our answer.