685. Redundant Connection II
HardSolutions in Python, Java, C++, TypeScript
Read the full problem statement on LeetCode and try it first. Below is how to approach it in an interview, followed by a complete answer in each language.
Approach
A rooted tree is a specific type of directed graph with two main properties:
Solution
Redundant Connection II in Python
class Solution:
def findRedundantDirectedConnection(self, edges: list[list[int]]) -> list[int]:
n = len(edges)
parent = [0] * (n + 1)
candA, candB = None, None
# Step 1: Check for a node with two parents
for edge in edges:
u, v = edge
if parent[v] != 0:
candA = [parent[v], v]
candB = edge
break
parent[v] = u
root = list(range(n + 1))
def find(i):
if root[i] != i:
root[i] = find(root[i])
return root[i]
def unite(i, j):
root_i = find(i)
root_j = find(j)
if root_i == root_j:
return False
root[root_i] = root_j
return True
if not candA:
# Case 1: No node with two parents, must be a cycle
redundant_edge = None
for u, v in edges:
if not unite(u, v):
redundant_edge = [u, v]
return redundant_edge
else:
# Case 2 & 3: A node has two parents
for u, v in edges:
if [u, v] == candB:
continue
if not unite(u, v):
# A cycle exists even without candB, so candA must be the problem.
return candA
# No cycle without candB, so candB is the redundant edge.
return candB
Redundant Connection II in Java
class Solution {
public int[] findRedundantDirectedConnection(int[][] edges) {
int n = edges.length;
int[] parent = new int[n + 1];
int[] candA = null, candB = null;
// Step 1: Check for a node with two parents
for (int[] edge : edges) {
int u = edge[0], v = edge[1];
if (parent[v] != 0) {
candA = new int[]{parent[v], v};
candB = edge;
break;
}
parent[v] = u;
}
DSU dsu = new DSU(n + 1);
if (candA == null) { // Case 1: No node with two parents, must be a cycle
int[] redundantEdge = null;
for (int[] edge : edges) {
if (!dsu.unite(edge[0], edge[1])) {
redundantEdge = edge;
}
}
return redundantEdge;
} else { // Case 2 & 3: A node has two parents
for (int[] edge : edges) {
if (edge[0] == candB[0] && edge[1] == candB[1]) continue;
if (!dsu.unite(edge[0], edge[1])) {
// A cycle exists even without candB, so candA must be the problem.
return candA;
}
}
// No cycle without candB, so candB is the redundant edge.
return candB;
}
}
class DSU {
int[] root;
DSU(int size) {
root = new int[size];
for (int i = 0; i < size; i++) root[i] = i;
}
int find(int i) {
if (root[i] == i) return i;
return root[i] = find(root[i]);
}
boolean unite(int i, int j) {
int rootI = find(i);
int rootJ = find(j);
if (rootI != rootJ) {
root[rootI] = rootJ;
return true;
}
return false;
}
}
}
Redundant Connection II in C++
class Solution {
public:
vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {
int n = edges.size();
vector<int> parent(n + 1, 0);
vector<int> candA, candB;
// Step 1: Check for a node with two parents
for (const auto& edge : edges) {
int u = edge[0], v = edge[1];
if (parent[v] != 0) {
candA = {parent[v], v};
candB = edge;
break;
}
parent[v] = u;
}
// Union-Find structure
vector<int> root(n + 1);
for (int i = 1; i <= n; ++i) root[i] = i;
auto find = [&](int i) {
while (i != root[i]) {
root[i] = root[root[i]]; // Path compression
i = root[i];
}
return i;
};
auto unite = [&](int i, int j) {
int rootI = find(i);
int rootJ = find(j);
if (rootI != rootJ) {
root[rootI] = rootJ;
return true;
}
return false;
};
if (candA.empty()) { // Case 1: No node with two parents, must be a cycle
vector<int> redundantEdge;
for (const auto& edge : edges) {
if (!unite(edge[0], edge[1])) {
redundantEdge = edge;
// We could break here, but problem says return the one that occurs LAST.
// Since one edge addition creates one cycle, any edge that fails to unite is on the cycle.
// The last one in the edges list is the answer.
}
}
return redundantEdge;
} else { // Case 2 & 3: A node has two parents
// Temporarily ignore candB and see if a cycle exists.
for (const auto& edge : edges) {
if (edge == candB) continue;
if (!unite(edge[0], edge[1])) {
// A cycle exists even without candB, so candA must be the problem.
return candA;
}
}
// No cycle without candB, so candB is the redundant edge.
return candB;
}
}
};
Redundant Connection II in TypeScript
function findRedundantDirectedConnection(edges: number[][]): number[] {
const n = edges.length;
const parent: number[] = new Array(n + 1).fill(0);
let candA: number[] | null = null;
let candB: number[] | null = null;
// Step 1: Check for a node with two parents
for (const edge of edges) {
const [u, v] = edge;
if (parent[v] !== 0) {
candA = [parent[v], v];
candB = edge;
break;
}
parent[v] = u;
}
const root: number[] = Array.from({ length: n + 1 }, (_, i) => i);
function find(i: number): number {
if (root[i] !== i) {
root[i] = find(root[i]);
}
return root[i];
}
function unite(i: number, j: number): boolean {
const rootI = find(i);
const rootJ = find(j);
if (rootI === rootJ) {
return false;
}
root[rootI] = rootJ;
return true;
}
if (candA === null) { // Case 1: No node with two parents, must be a cycle
let redundantEdge: number[] = [];
for (const edge of edges) {
if (!unite(edge[0], edge[1])) {
redundantEdge = edge;
}
}
return redundantEdge;
} else { // Case 2 & 3: A node has two parents
for (const edge of edges) {
if (edge === candB) {
continue;
}
if (!unite(edge[0], edge[1])) {
// A cycle exists even without candB, so candA must be the problem.
return candA;
}
}
// No cycle without candB, so candB is the redundant edge.
return candB!;
}
}
More Hard LeetCode answers
Preparing for interviews beyond LeetCode? Practise system design, SQL and behavioral questions with an in-browser runner on KatInterview.
LeetCode is a trademark of LeetCode LLC. KatInterview is not affiliated with LeetCode.