Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Home
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets

Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Talentd Logo
Talentd

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
Jobs
Learning
Career Tools
Talentd Logo
Talentd#1 Freshers Platform
DSA Corner
DashboardQuestionsTopicsCompaniesSheets
Back to Problems

685. Redundant Connection II

Hard34.8% Acceptance
Depth-First SearchBreadth-First SearchUnion Find
Asked by:
Amazon
ProblemSolutions (12)VideosCompanies (1)Notes

Problem Statement

In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) for which all other nodes are descendants of this node, plus every node has exactly one parent, except for the root node which has no parents.

The given input is a directed graph that started as a rooted tree with n nodes (with distinct values from 1 to n), with one additional directed edge added. The added edge has two different vertices chosen from 1 to n, and was not an edge that already existed.

The resulting graph is given as a 2D-array of edges. Each element of edges is a pair [ui, vi] that represents a directed edge connecting nodes ui and vi, where ui is a parent of child vi.

Return an edge that can be removed so that the resulting graph is a rooted tree of n nodes. If there are multiple answers, return the answer that occurs last in the given 2D-array.

Example 1:

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

Example 2:

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

Constraints:

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

Your trusted platform to ace any job interviews, craft the perfect resumes, and land your dream jobs.

P
Featured on
Product Hunt
â–²455
All services are online

Products

  • Resume Review
  • Company Prep Pack
  • DSA Corner
  • Jobs
  • Internships
  • Fresher Jobs
  • Roadmaps
  • Tax Calculator

Resources

  • Articles
  • DRDO Internships

Support

  • Contact Us

DSA & Interview Prep

  • DSA Questions
  • DSA Sheets
  • Company Questions
  • Topics

Company

  • Companies Hiring
  • About
  • Contact
  • Advertisement

Legal

  • Privacy Policy
  • Terms & Conditions
  • Refund Policy
  • Delivery Policy

Popular Skills

Browse All Skills →

Popular Tags

Browse All Tags →

© 2025 Talentd.in - All rights reserved

Privacy PolicyTerms & Conditions
A

Approach

Redundant Connection II asks you to identify an edge that should be removed so that a directed graph becomes a valid rooted tree. The challenge comes from two possible issues: a node having two parents, or the presence of a cycle. A common strategy combines parent tracking with the Union-Find (Disjoint Set) data structure.

First, scan edges to detect whether any node has two incoming edges. If such a case appears, temporarily record the conflicting edges. Then use Union-Find to process edges and check whether adding an edge forms a cycle. Depending on whether a cycle appears and whether a node had two parents, you can determine which edge must be removed.

This approach efficiently separates the two failure cases (cycle vs. double parent) and resolves them with near-constant-time union operations. The algorithm runs in approximately O(n * α(n)) time using Union-Find, where α is the inverse Ackermann function, with O(n) space for parent and rank structures.

Complexity

ApproachTime ComplexitySpace Complexity
Union-Find with parent trackingO(n * α(n))O(n)
Graph traversal validation (DFS/BFS)O(n)O(n)

Video Solution Available

NeetCode

View all video solutions

Solutions (12)

Union-Find with Parent Check

This approach uses a Union-Find (Disjoint Set Union, DSU) structure to detect cycles and check for nodes with two parents. The goal is to handle two situations: a node having two parents, and a cycle existing in the graph. We iterate through the edges to identify a node with two parents and mark the offending edge. Then, we use the Union-Find structure to track cycles and find the redundant connection based on the identified edges.

Time Complexity: O(n), where n is the number of edges.

Space Complexity: O(n), for storing the parent and rank arrays.

CC++JavaPythonC#JavaScript
1#include <stdio.h>
2#include <stdlib.h>
3
4typedef struct UnionFind {
5    int* parent;
6    int




Explanation

The implementation uses a Union-Find data structure to manage connectivity among nodes and track potential redundant connections by checking parent-child relationships and detecting cycles. First, it checks whether any node has two parents. If found, it temporarily removes that edge and continues to apply Union-Find to check for any cycles. If a cycle exists without finding a node with two parents, the wrong edge is part of the cycle. Otherwise, it'll be the edge removed previously.

Graph Traversal & Cycle Detection

In this method, we focus on identifying two scenarios: an edge creating a cycle in the graph and a node with two parents. With graph traversal, primarily cross-check with parent pointers and DFS for cycle confirmation, fine-tuning insights to pinpoint a last array occurrence redundant connection.

Time Complexity: O(n^2) with recursive verification.

Space Complexity: O(n), memoizing visited nodes.

CC++JavaPythonC#JavaScript
1#




Video Solutions

Watch expert explanations and walkthroughs

Redundant Connection - Union Find - Leetcode 684 - Python

NeetCode
16:04123,096 views

Asked By Companies

1 companies
A
Amazon

Prepare for Interviews

Practice problems asked by these companies to ace your technical interviews.

Explore More Problems

Notes

Personal Notes

Jot down your thoughts, approach, and key learnings

0 characters

Similar Problems

Same TreeEasy
Symmetric TreeEasy
Maximum Depth of Binary TreeEasy
Minimum Depth of Binary TreeEasy
More similar problems

Related Topics

Depth-First SearchBreadth-First SearchUnion FindGraph

Problem Stats

Acceptance Rate34.8%
DifficultyHard
Companies1

Practice on LeetCode

Solve with full IDE support and test cases

Solve Now

Frequently Asked Questions

Why is Redundant Connection II considered difficult?

The problem is challenging because it combines two separate graph issues: cycle detection and nodes with two parents. Handling both cases correctly requires careful logic and often a hybrid strategy involving Union-Find and parent tracking.

Is Redundant Connection II asked in FAANG interviews?

Yes, variations of cycle detection and Union-Find problems frequently appear in FAANG-style interviews. Redundant Connection II is particularly valuable because it tests understanding of directed graphs, disjoint sets, and edge-case reasoning.

What is the optimal approach for Redundant Connection II?

The optimal approach uses Union-Find combined with parent tracking. First detect if any node has two parents, then run Union-Find to identify cycles. By analyzing these two conditions, you can determine which edge must be removed.

What data structure is best for solving Redundant Connection II?

The Union-Find (Disjoint Set Union) data structure is the most effective for detecting cycles efficiently. It supports near constant-time union and find operations, making it ideal for incremental edge processing in graph problems.

*
rank
;
7
}
UnionFind
;
8
9
UnionFind
*
createUnionFind
(
int
size
)
{
10
UnionFind
*
uf
=
(
UnionFind
*
)
malloc
(
sizeof
(
UnionFind
)
)
;
11
uf
->
parent
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
size
)
;
12
uf
->
rank
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
size
)
;
13
for
(
int
i
=
0
;
i
<
size
;
i
++
)
{
14
uf
->
parent
[
i
]
=
i
;
15
uf
->
rank
[
i
]
=
1
;
16
}
17
return
uf
;
18
}
19
20
int
find
(
UnionFind
*
uf
,
int
x
)
{
21
if
(
uf
->
parent
[
x
]
!=
x
)
{
22
uf
->
parent
[
x
]
=
find
(
uf
,
uf
->
parent
[
x
]
)
;
23
}
24
return
uf
->
parent
[
x
]
;
25
}
26
27
void
unionSets
(
UnionFind
*
uf
,
int
x
,
int
y
)
{
28
int
rootX
=
find
(
uf
,
x
)
;
29
int
rootY
=
find
(
uf
,
y
)
;
30
if
(
rootX
!=
rootY
)
{
31
if
(
uf
->
rank
[
rootX
]
>
uf
->
rank
[
rootY
]
)
{
32
uf
->
parent
[
rootY
]
=
rootX
;
33
}
else
if
(
uf
->
rank
[
rootX
]
<
uf
->
rank
[
rootY
]
)
{
34
uf
->
parent
[
rootX
]
=
rootY
;
35
}
else
{
36
uf
->
parent
[
rootY
]
=
rootX
;
37
uf
->
rank
[
rootX
]
++
;
38
}
39
}
40
}
41
42
int
*
findRedundantDirectedConnection
(
int
*
*
edges
,
int
edgesSize
,
int
*
edgesColSize
,
int
*
returnSize
)
{
43
int
n
=
edgesSize
;
44
int
*
parent
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
(
n
+
1
)
)
;
45
for
(
int
i
=
0
;
i
<=
n
;
i
++
)
{
46
parent
[
i
]
=
i
;
47
}
48
int
*
can1
=
NULL
;
49
int
*
can2
=
NULL
;
50
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
51
int
u
=
edges
[
i
]
[
0
]
,
v
=
edges
[
i
]
[
1
]
;
52
if
(
parent
[
v
]
!=
v
)
{
53
can1
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
54
can2
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
55
can1
[
0
]
=
parent
[
v
]
;
56
can1
[
1
]
=
v
;
57
can2
[
0
]
=
u
;
58
can2
[
1
]
=
v
;
59
edges
[
i
]
[
1
]
=
0
;
60
}
else
{
61
parent
[
v
]
=
u
;
62
}
63
}
64
65
UnionFind
*
uf
=
createUnionFind
(
n
+
1
)
;
66
for
(
int
i
=
0
;
i
<
n
;
i
++
)
{
67
if
(
edges
[
i
]
[
1
]
==
0
)
continue
;
68
int
u
=
edges
[
i
]
[
0
]
,
v
=
edges
[
i
]
[
1
]
;
69
if
(
find
(
uf
,
u
)
==
find
(
uf
,
v
)
)
{
70
if
(
can1
)
return
can1
;
71
int
*
result
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
72
result
[
0
]
=
u
;
73
result
[
1
]
=
v
;
74
return
result
;
75
}
76
unionSets
(
uf
,
u
,
v
)
;
77
}
78
return
can2
;
79
}
80
include
<stdio.h>
2
#
include
<stdlib.h>
3
#
include
<stdbool.h>
4
5
void
dfs
(
int
*
parent
,
bool
*
visited
,
bool
*
cycle
,
int
current
)
{
6
visited
[
current
]
=
true
;
7
int
next
=
parent
[
current
]
;
8
if
(
next
!=
0
)
{
9
if
(
!
visited
[
next
]
)
{
10
dfs
(
parent
,
visited
,
cycle
,
next
)
;
11
}
else
{
12
*
cycle
=
true
;
13
}
14
}
15
}
16
17
int
*
findRedundantDirectedConnection
(
int
*
*
edges
,
int
edgesSize
,
int
*
edgesColSize
,
int
*
returnSize
)
{
18
int
n
=
edgesSize
;
19
int
*
parent
=
(
int
*
)
calloc
(
n
+
1
,
sizeof
(
int
)
)
;
20
21
for
(
int
i
=
0
;
i
<
n
;
++
i
)
{
22
int
u
=
edges
[
i
]
[
0
]
,
v
=
edges
[
i
]
[
1
]
;
23
if
(
parent
[
v
]
!=
0
)
{
24
int
*
can1
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
25
int
*
can2
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
26
can1
[
0
]
=
parent
[
v
]
;
27
can1
[
1
]
=
v
;
28
can2
[
0
]
=
u
;
29
can2
[
1
]
=
v
;
30
parent
[
v
]
=
0
;
31
bool cycle
=
false
;
32
for
(
int
j
=
0
;
j
<=
n
;
j
++
)
{
33
if
(
parent
[
j
]
==
0
)
continue
;
34
bool
*
visited
=
(
bool
*
)
calloc
(
n
+
1
,
sizeof
(
bool
)
)
;
35
dfs
(
parent
,
visited
,
&
cycle
,
j
)
;
36
free
(
visited
)
;
37
if
(
cycle
)
break
;
38
}
39
if
(
cycle
)
return
can1
;
40
else
return
can2
;
41
}
42
parent
[
v
]
=
u
;
43
}
44
45
bool cycle
=
false
;
46
for
(
int
j
=
0
;
j
<=
n
;
j
++
)
{
47
if
(
parent
[
j
]
==
0
)
continue
;
48
bool
*
visited
=
(
bool
*
)
calloc
(
n
+
1
,
sizeof
(
bool
)
)
;
49
dfs
(
parent
,
visited
,
&
cycle
,
j
)
;
50
free
(
visited
)
;
51
if
(
cycle
)
{
52
int
u
=
parent
[
j
]
,
v
=
j
;
53
int
*
result
=
(
int
*
)
malloc
(
sizeof
(
int
)
*
2
)
;
54
result
[
0
]
=
u
;
55
result
[
1
]
=
v
;
56
return
result
;
57
}
58
}
59
60
return
NULL
;
61
}
62

Explanation

Utilizing DFS, this C code ensures settings target node connections and traces for potential overlapping cycles, using indications to determine redundancy. Understanding traversal in view of directed graph properties is fundamental to isolating missteps.