LeetCode 684 Redundant Connection - Why Naive Fails

The trap

Try removing each edge and check if the remaining graph is a tree (connected with no cycles).

For each of nn edges, checking connectivity takes O(n)O(n). Total: O(n2)O(n^2).

Can you detect the cycle-forming edge as you process edges?