Problem
A tree is an undirected graph with a path between each pair of nodes and no cycle. A cycle is a path that starts and ends at the same node.
The graph in this problem started as a tree with n nodes, which have the numbers 1 to n. Then one extra edge joined two different nodes that had no edge between them.
You get the result as a list edges with n items. Each item [a, b] is an edge between node a and node b.
Return an edge that you can remove to get a tree of all n nodes again. If more than one edge is correct, return the edge that occurs last in edges.
Example 1
- Input:
- edges = [[2,3],[1,2],[3,4],[1,4],[4,5]]
- Output:
- [1,4]
- Explanation:
- The edges
[2, 3],[1, 2],[3, 4]and[1, 4]make a cycle. You can remove any of them, and[1, 4]occurs last inedges.
Example 2
- Input:
- edges = [[3,4],[1,3],[1,4],[2,4]]
- Output:
- [1,4]
- Explanation:
- The cycle uses the nodes 1, 3 and 4. The edge
[2, 4]is not in the cycle, and node 2 has no other edge.
Constraints
- n == edges.length
- 3 <= n <= 1000
- edges[i].length == 2
- 1 <= a < b <= n
- No edge occurs two times
- The graph is connected
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
No company association
Leetcode 684. Redundant Connection
Reported date not specified · 0 source reports
Source snapshot: Nov 12, 2026
View source