LeetCode Problem

How to Solve Redundant Connection II

This problem involves detecting and removing the redundant edge from a graph that was initially a rooted tree. You are given a graph with one extra edge, which causes a cycle. Using graph traversal techniques, the goal is to identify and remove the extra edge that creates this cycle. Depth-first search (DFS) is crucial in solving this problem efficiently.

GhostInterview Help

Need help with Redundant Connection II without spending extra time grinding it?

GhostInterview can read Redundant Connection II from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #685Graph traversal with depth-first searchReviewed 2026-03-07
Difficulty
Hard
Primary pattern
Graph traversal with depth-first search
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem involves detecting and removing the redundant edge from a graph that was initially a rooted tree. You are given a graph with one extra edge, which causes a cycle. Using graph traversal techniques, the goal is to identify and remove the extra edge that creates this cycle. Depth-first search (DFS) is crucial in solving this problem efficiently.

Problem Statement

You are given a directed graph with n nodes. Initially, the graph is a rooted tree, but one additional directed edge has been added. The graph contains exactly one cycle, and you need to find the redundant connection that causes this cycle and return it. The graph is represented by a 2D array, where each element [u, v] indicates a directed edge from node u to node v.

Your task is to identify and return the redundant connection. Since there is exactly one redundant edge, you can safely assume that the input graph is guaranteed to contain one and only one cycle. The problem should be solved using graph traversal techniques such as DFS or BFS.

Examples

Example 1

Input: edges = [[1,2],[1,3],[2,3]]

Output: [2,3]

Example details omitted.

Example 2

Input: edges = [[1,2],[2,3],[3,4],[4,1],[1,5]]

Output: [4,1]

Example details omitted.

Constraints

  • n == edges.length
  • 3 <= n <= 1000
  • edges[i].length == 2
  • 1 <= ui, vi <= n
  • ui != vi

Solution Approach

DFS for Cycle Detection

Use depth-first search (DFS) to traverse the graph. As you visit nodes, keep track of the visited nodes to detect cycles. Once a cycle is detected, the last edge added to the cycle is the redundant connection.

Union-Find (Disjoint Set) for Efficient Tracking

Union-Find is another efficient approach for cycle detection. As you iterate through the edges, union the nodes. If two nodes are already in the same set, the current edge is redundant and causes the cycle.

BFS for Detecting Redundant Edges

Breadth-first search (BFS) can also be applied to find the redundant connection. Traverse the graph level by level, tracking visited nodes. If a node is visited again, it indicates the cycle and the edge leading to it is redundant.

Complexity Analysis

MetricValue
TimeO(N)
SpaceO(N)

The time complexity for both DFS and Union-Find approaches is O(N), where N is the number of nodes. The space complexity is also O(N) due to the storage required for tracking visited nodes and sets in Union-Find.

What Interviewers Usually Probe

  • The candidate uses an efficient cycle detection algorithm like DFS or Union-Find.
  • The candidate is able to explain the graph traversal approach clearly and apply it correctly.
  • The candidate identifies and removes the redundant edge without unnecessary computation.

Common Pitfalls or Variants

Common pitfalls

  • Not properly tracking the visited nodes, leading to incorrect cycle detection.
  • Confusing Union-Find operations, such as not correctly implementing union and find operations.
  • Failing to handle edge cases where the graph has only a single redundant connection.

Follow-up variants

  • Graph with multiple redundant edges (not allowed in this problem but can be extended to handle).
  • Using BFS instead of DFS for traversal and cycle detection.
  • Handling larger graphs with optimizations in both space and time complexity.

How GhostInterview Helps

  • GhostInterview walks you through step-by-step graph traversal techniques like DFS and Union-Find.
  • It highlights key failure modes such as incorrect cycle detection and Union-Find mistakes.
  • GhostInterview's interactive solver helps you optimize your approach for efficiency and correctness.

Topic Pages

FAQ

How do I detect the redundant edge in a directed graph?

Use depth-first search or Union-Find to detect the cycle caused by the redundant edge. The last edge that completes the cycle is the redundant one.

Can BFS be used to solve the Redundant Connection II problem?

Yes, BFS can be used for cycle detection by traversing the graph level by level and identifying the first revisited node, which forms the cycle.

What are the main patterns used to solve Redundant Connection II?

Graph traversal techniques, specifically DFS, BFS, and Union-Find, are the main patterns used to detect the redundant connection.

What is the time complexity of solving Redundant Connection II?

The time complexity is O(N), where N is the number of nodes, for both DFS and Union-Find approaches.

How does GhostInterview help with solving Redundant Connection II?

GhostInterview provides step-by-step explanations of graph traversal techniques, helping you detect cycles and find the redundant edge with ease.

GhostInterview Solver

Need direct help with Redundant Connection II instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Redundant Connection II from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.