Graph LeetCode Problems: All 138,
With Python Solutions

Every problem in this library that LeetCode tags Graph. 106 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
3
Medium
64
Hard
71
1 pattern

How Graph problems are solved

A tag names the subject, not the method. These pattern hubs cover the techniques that actually solve Graph problems — each one explains the approach, gives a Python template and states its complexity.

Eyes only

When the Graph problem arrives live.

In the live round, Stealth Interview reads the problem off your screen, returns a working solution with a step-by-step explanation and its time and space complexity, and transcribes what the interviewer says. It's a desktop app for macOS and Windows, invisible to screen sharing.

Graph problems by difficulty

Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (3)

Easy problems
No.Problem
0997Find the Town Judge
1791Find Center of Star Graph
1971Find if Path Exists in Graph

Medium (64)

Medium problems
No.Problem
0133Clone Graph
0207Course Schedule
0210Course Schedule II
0310Minimum Height Trees
0399Evaluate Division
0547Number of Provinces
0684Redundant Connection
0743Network Delay Time
0785Is Graph Bipartite?
0787Cheapest Flights Within K Stops
0797All Paths From Source to Target
0802Find Eventual Safe States
0841Keys and Rooms
0851Loud and Rich
0886Possible Bipartition
0947Most Stones Removed with Same Row or Column
0990Satisfiability of Equality Equations
1042Flower Planting With No Adjacent
1129Shortest Path with Alternating Colors
1311Get Watched Videos by Your Friends
1319Number of Operations to Make Network Connected
1334Find the City With the Smallest Number of Neighbors at a Threshold Distance
1361Validate Binary Tree Nodes
1462Course Schedule IV
1466Reorder Routes to Make All Paths Lead to the City Zero
1514Path with Maximum Probability
1557Minimum Number of Vertices to Reach All Nodes
1584Min Cost to Connect All Points
1615Maximal Network Rank
1786Number of Restricted Paths From First to Last Node
1976Number of Ways to Arrive at Destination
2039The Time When the Network Becomes Idle
2101Detonate the Maximum Bombs
2115Find All Possible Recipes from Given Supplies
2192All Ancestors of a Node in a Directed Acyclic Graph
2285Maximum Total Importance of Roads
2316Count Unreachable Pairs of Nodes in an Undirected Graph
2359Find Closest Node to Given Two Nodes
2368Reachable Nodes With Restrictions
2374Node With Highest Edge Score
2467Most Profitable Path in a Tree
2477Minimum Fuel Cost to Report to the Capital
2492Minimum Score of a Path Between Two Cities
2497Maximum Star Sum of a Graph
2662Minimum Cost of a Path With Special Roads
2685Count the Number of Complete Components
2924Find Champion II
2976Minimum Cost to Convert String I
0261Graph Valid TreePremium
0277Find the CelebrityPremium
0323Number of Connected Components in an Undirected GraphPremium
0444Sequence ReconstructionPremium
0505The Maze IIPremium
1059All Paths from Source Lead to DestinationPremium
1135Connecting Cities With Minimum CostPremium
1136Parallel CoursesPremium
1245Tree DiameterPremium
1810Minimum Path Cost in a Hidden GridPremium
1820Maximum Number of Accepted InvitationsPremium
2077Paths in Maze That Lead to Same RoomPremium
2093Minimum Cost to Reach City With DiscountsPremium
2297Jump Game VIIIPremium
2473Minimum Cost to Buy ApplesPremium
2737Find the Closest Marked NodePremium

Hard (71)

Hard problems
No.Problem
0329Longest Increasing Path in a Matrix
0332Reconstruct Itinerary
0685Redundant Connection II
0753Cracking the Safe
0765Couples Holding Hands
0834Sum of Distances in Tree
0847Shortest Path Visiting All Nodes
0882Reachable Nodes In Subdivided Graph
0913Cat and Mouse
0924Minimize Malware Spread
0928Minimize Malware Spread II
1192Critical Connections in a Network
1203Sort Items by Groups Respecting Dependencies
1298Maximum Candies You Can Get from Boxes
1368Minimum Cost to Make at Least One Valid Path in a Grid
1377Frog Position After T Seconds
1489Find Critical and Pseudo-Critical Edges in Minimum Spanning Tree
1494Parallel Courses II
1579Remove Max Number of Edges to Keep Graph Fully Traversable
1632Rank Transform of a Matrix
1697Checking Existence of Edge Length Limited Paths
1719Number Of Ways To Reconstruct A Tree
1728Cat and Mouse II
1761Minimum Degree of a Connected Trio in a Graph
1782Count Pairs Of Nodes
1857Largest Color Value in a Directed Graph
1916Count Ways to Build Rooms in an Ant Colony
1928Minimum Cost to Reach Destination in Time
2045Second Minimum Time to Reach Destination
2050Parallel Courses III
2065Maximum Path Quality of a Graph
2076Process Restricted Friend Requests
2092Find All People With Secret
2127Maximum Employees to Be Invited to a Meeting
2203Minimum Weighted Subgraph With the Required Paths
2242Maximum Score of a Node Sequence
2246Longest Path With Different Adjacent Characters
2290Minimum Obstacle Removal to Reach Corner
2328Number of Increasing Paths in a Grid
2360Longest Cycle in a Graph
2392Build a Matrix With Conditions
2421Number of Good Paths
2493Divide Nodes Into the Maximum Number of Groups
2508Add Edges to Make Degrees of All Nodes Even
2577Minimum Time to Visit a Cell In a Grid
2603Collect Coins in a Tree
2608Shortest Cycle in a Graph
2642Design Graph With Shortest Path Calculator
2646Minimize the Total Price of the Trips
2699Modify Graph Edge Weights
2846Minimum Edge Weight Equilibrium Queries in a Tree
2858Minimum Edge Reversals So Every Node Is Reachable
2876Count Visited Nodes in a Directed Graph
2959Number of Possible Sets of Closing Branches
2977Minimum Cost to Convert String II
0269Alien DictionaryPremium
0499The Maze IIIPremium
0631Design Excel Sum FormulaPremium
1153String Transforms Into Another StringPremium
1168Optimize Water Distribution in a VillagePremium
1548The Most Similar Path in a GraphPremium
1591Strange Printer II
1724Checking Existence of Edge Length Limited Paths IIPremium
2097Valid Arrangement of Pairs
2123Minimum Operations to Remove Adjacent Ones in MatrixPremium
2204Distance to a Cycle in Undirected GraphPremium
2247Maximum Cost of Trip With K HighwaysPremium
2307Check for Contradictions in EquationsPremium
2371Minimize Maximum Value in a GridPremium
2479Maximum XOR of Two Non-Overlapping SubtreesPremium
2714Find Shortest Path with K HopsPremium