Stealth Interview
  • Features
  • Pricing
  • Blog
  • Login
  • Sign up

Leetcode #1489: Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree

In this guide, we solve Leetcode #1489 Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree in Python and focus on the core idea that makes the solution efficient.

You will see the intuition, the step-by-step method, and a clean Python implementation you can use in interviews.

Leetcode

Problem Statement

Given a weighted undirected connected graph with n vertices numbered from 0 to n - 1, and an array edges where edges[i] = [ai, bi, weighti] represents a bidirectional and weighted edge between nodes ai and bi. A minimum spanning tree (MST) is a subset of the graph's edges that connects all vertices without cycles and with the minimum possible total edge weight.

Quick Facts

  • Difficulty: Hard
  • Premium: No
  • Tags: Union Find, Graph, Minimum Spanning Tree, Sorting, Strongly Connected Component

Intuition

The data forms a graph, so we should explore nodes and edges systematically.

A traversal ensures we visit each node once while maintaining the needed state.

Approach

Build an adjacency list and traverse with BFS or DFS.

Aggregate results as you visit nodes.

Steps:

  • Build the graph.
  • Traverse with BFS/DFS.
  • Accumulate the required output.

Example

Input: n = 5, edges = [[0,1,1],[1,2,1],[2,3,2],[0,3,2],[0,4,3],[3,4,3],[1,4,6]] Output: [[0,1],[2,3,4,5]] Explanation: The figure above describes the graph. The following figure shows all the possible MSTs: Notice that the two edges 0 and 1 appear in all MSTs, therefore they are critical edges, so we return them in the first list of the output. The edges 2, 3, 4, and 5 are only part of some MSTs, therefore they are considered pseudo-critical edges. We add them to the second list of the output.

Python Solution

class UnionFind: def __init__(self, n): self.p = list(range(n)) self.n = n def union(self, a, b): if self.find(a) == self.find(b): return False self.p[self.find(a)] = self.find(b) self.n -= 1 return True def find(self, x): if self.p[x] != x: self.p[x] = self.find(self.p[x]) return self.p[x] class Solution: def findCriticalAndPseudoCriticalEdges( self, n: int, edges: List[List[int]] ) -> List[List[int]]: for i, e in enumerate(edges): e.append(i) edges.sort(key=lambda x: x[2]) uf = UnionFind(n) v = sum(w for f, t, w, _ in edges if uf.union(f, t)) ans = [[], []] for f, t, w, i in edges: uf = UnionFind(n) k = sum(z for x, y, z, j in edges if j != i and uf.union(x, y)) if uf.n > 1 or (uf.n == 1 and k > v): ans[0].append(i) continue uf = UnionFind(n) uf.union(f, t) k = w + sum(z for x, y, z, j in edges if j != i and uf.union(x, y)) if k == v: ans[1].append(i) return ans

Complexity

The time complexity is O(V+E). The space complexity is O(V).

Edge Cases and Pitfalls

Watch for boundary values, empty inputs, and duplicate values where applicable. If the problem involves ordering or constraints, confirm the invariant is preserved at every step.

Summary

This Python solution focuses on the essential structure of the problem and keeps the implementation interview-friendly while meeting the constraints.


Ace your next coding interview

We're here to help you ace your next coding interview.

Subscribe
Stealth Interview
© 2026 Stealth Interview®Stealth Interview is a registered trademark. All rights reserved.
Product
  • Blog
  • Pricing
Company
  • Terms of Service
  • Privacy Policy