medium

Redundant Connection

A tree with n nodes had one extra undirected edge added, creating exactly one cycle. Given the resulting edge list, return the edge that can be removed to restore a tree; if several qualify, return the one that appears last in the input.

Constraints
  • 3 ≤ n ≤ 1000
  • edges.length = n
  • No repeated edges
Examples
in: edges = [[1,2],[1,3],[2,3]]
out: [2,3]
in: edges = [[1,2],[2,3],[3,4],[1,4],[1,5]]
out: [1,4]

Code it yourself

Solve in
Test execution is not yet available for this exercise.Practice journal →Draft saved in this browser.
Hints:
Which approach applies?

Choose an approach to check your pattern recognition, or reveal the discussion when you need help.