</>

684. Redundant Connection

Medium NeetCode 150
Graphs Time O(N * α(N) Space O(N)
↗ Open on LeetCode Source: NeetCode 150

Quick review

Pattern
Graphs
Time
O(N * α(N)
Space
O(N)

Standard Solution (C++)

SummaryGiven a list of edges forming a tree on n nodes with exactly one extra edge added, find and return that single redundant edge — the one whose removal would restore a valid tree.

Approach
  • Use Union-Find (Disjoint Set Union) with path compression and union-by-rank.
  • Initialize each node as its own parent. Process edges in order; for each edge, find the roots of both endpoints.
  • If both endpoints share the same root, a cycle would be formed — this edge is redundant; return it immediately.
  • Otherwise, merge the two components by attaching the smaller-rank tree under the larger-rank root (union-by-rank), keeping the structure nearly flat.
#include <vector>
using namespace std;

class Solution {
    // Find root with path compression
    int find(vector<int>& parent, int x) {
        if (parent[x] != x)
            parent[x] = find(parent, parent[x]); // path compression
        return parent[x];
    }

    // Union by rank; returns false if x and y are already in the same set
    bool unite(vector<int>& parent, vector<int>& rank, int x, int y) {
        int rx = find(parent, x), ry = find(parent, y);
        if (rx == ry) return false; // cycle detected
        if (rank[rx] < rank[ry]) swap(rx, ry);
        parent[ry] = rx;            // attach smaller tree under larger root
        if (rank[rx] == rank[ry]) ++rank[rx];
        return true;
    }

public:
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        int n = static_cast<int>(edges.size());
        vector<int> parent(n + 1), rank(n + 1, 0);
        for (int i = 1; i <= n; ++i) parent[i] = i; // each node is its own root

        for (const auto& e : edges) {
            if (!unite(parent, rank, e[0], e[1]))
                return e; // this edge creates a cycle — it is redundant
        }
        return {}; // guaranteed not reached per problem constraints
    }
};
Time O(n · α(n)) where α is the inverse Ackermann function — effectively O(n) Space O(n)

A canonical reference solution. Problem statement © LeetCode — see the official link above.

My Solution (C++)

class Solution {
public:
    int find(vector<int>& parents, int idx){
        if(parents[idx]==idx){
            return idx;
        }
        return parents[idx]=find(parents, parents[idx]);
    }
    vector<int> findRedundantConnection(vector<vector<int>>& edges) {
        int n=edges.size();
        vector<int> parents(n+1);
        vector<int> ranks(n+1, 1);

        for(int i=1; i<=n; ++i){
            parents[i]=i;
        }

        for(const auto& e: edges){
            // find parents
            int a=e[0];
            int b=e[1];

            int pa=find(parents, a);
            int pb=find(parents, b);

            // if same: return this edge
            if(pa==pb){
                return e;
            }
            // if not same: join
            if(ranks[pa]>ranks[pb]){
                parents[pb]=pa;
            }
            else if(ranks[pb]>ranks[pa]){
                parents[pa]=pb;
            }
            else{ // rank: pa==pb
                parents[pb]=pa;
                ++ranks[pa];
            }
            
        }

        return {};
    }
};

// Time Complexity: O(N * α(N)), where α is the Inverse Ackermann function, can be considered nearly constant.
// Space Complexity: O(N)
Time O(N * α(N) Space O(N)

My own solution and notes, as I wrote them.

▶ Run it — editable in-browser playground with a sample driver C++

Your solution plus an auto-generated main() and sample input so Run produces output. The driver and inputs are unverified — edit freely; Reset restores this starting point.

C++ ▶ Runnable Auto-generated driver with sample input — unverified.

Runs in a free public sandbox (Wandbox). Output and timing include network latency; don't paste secrets.

Related — same pattern