Depth-First Search LeetCode Problems: All 289,
With Python Solutions

Every problem in this library that LeetCode tags Depth-First Search. 216 of them carry a complete Python solution, a worked example and the time and space complexity of the approach.

Easy
39
Medium
185
Hard
65
1 pattern

How Depth-First Search problems are solved

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

Eyes only

When the Depth-First Search 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.

Depth-First Search problems by difficulty

Showing the first 200 of 289 problems. Problems with a complete Python solution are listed first, then by ascending problem number.

Easy (36)

Easy problems
No.Problem
0094Binary Tree Inorder Traversal
0100Same Tree
0101Symmetric Tree
0104Maximum Depth of Binary Tree
0110Balanced Binary Tree
0111Minimum Depth of Binary Tree
0112Path Sum
0144Binary Tree Preorder Traversal
0145Binary Tree Postorder Traversal
0226Invert Binary Tree
0257Binary Tree Paths
0404Sum of Left Leaves
0463Island Perimeter
0501Find Mode in Binary Search Tree
0530Minimum Absolute Difference in BST
0543Diameter of Binary Tree
0559Maximum Depth of N-ary Tree
0563Binary Tree Tilt
0572Subtree of Another Tree
0589N-ary Tree Preorder Traversal
0590N-ary Tree Postorder Traversal
0617Merge Two Binary Trees
0637Average of Levels in Binary Tree
0653Two Sum IV - Input is a BST
0671Second Minimum Node In a Binary Tree
0733Flood Fill
0783Minimum Distance Between BST Nodes
0872Leaf-Similar Trees
0897Increasing Order Search Tree
0938Range Sum of BST
0965Univalued Binary Tree
0993Cousins in Binary Tree
1022Sum of Root To Leaf Binary Numbers
1379Find a Corresponding Node of a Binary Tree in a Clone of That Tree
1971Find if Path Exists in Graph
2331Evaluate Boolean Binary Tree

Medium (127)

Medium problems
No.Problem
0079Word Search
0098Validate Binary Search Tree
0099Recover Binary Search Tree
0113Path Sum II
0114Flatten Binary Tree to Linked List
0116Populating Next Right Pointers in Each Node
0117Populating Next Right Pointers in Each Node II
0129Sum Root to Leaf Numbers
0130Surrounded Regions
0133Clone Graph
0199Binary Tree Right Side View
0200Number of Islands
0207Course Schedule
0210Course Schedule II
0211Design Add and Search Words Data Structure
0230Kth Smallest Element in a BST
0235Lowest Common Ancestor of a Binary Search Tree
0236Lowest Common Ancestor of a Binary Tree
0310Minimum Height Trees
0337House Robber III
0341Flatten Nested List Iterator
0365Water and Jug Problem
0385Mini Parser
0386Lexicographical Numbers
0388Longest Absolute File Path
0399Evaluate Division
0417Pacific Atlantic Water Flow
0419Battleships in a Board
0430Flatten a Multilevel Doubly Linked List
0437Path Sum III
0449Serialize and Deserialize BST
0508Most Frequent Subtree Sum
0513Find Bottom Left Tree Value
0515Find Largest Value in Each Tree Row
0529Minesweeper
0538Convert BST to Greater Tree
0547Number of Provinces
0565Array Nesting
0606Construct String from Binary Tree
0623Add One Row to Tree
0652Find Duplicate Subtrees
0655Print Binary Tree
0662Maximum Width of Binary Tree
0669Trim a Binary Search Tree
0672Bulb Switcher II
0676Implement Magic Dictionary
0684Redundant Connection
0687Longest Univalue Path
0690Employee Importance
0695Max Area of Island
0721Accounts Merge
0743Network Delay Time
0785Is Graph Bipartite?
0787Cheapest Flights Within K Stops
0797All Paths From Source to Target
0802Find Eventual Safe States
0814Binary Tree Pruning
0841Keys and Rooms
0851Loud and Rich
0863All Nodes Distance K in Binary Tree
0865Smallest Subtree with all the Deepest Nodes
0886Possible Bipartition
0934Shortest Bridge
0947Most Stones Removed with Same Row or Column
0951Flip Equivalent Binary Trees
0959Regions Cut By Slashes
0971Flip Binary Tree To Match Preorder Traversal
0979Distribute Coins in Binary Tree
0988Smallest String Starting From Leaf
1020Number of Enclaves
1026Maximum Difference Between Node and Ancestor
1034Coloring A Border
1038Binary Search Tree to Greater Sum Tree
1042Flower Planting With No Adjacent
1080Insufficient Nodes in Root to Leaf Paths
1110Delete Nodes And Return Forest
1123Lowest Common Ancestor of Deepest Leaves
1145Binary Tree Coloring Game
1161Maximum Level Sum of a Binary Tree
1202Smallest String With Swaps
1233Remove Sub-Folders from the Filesystem
1254Number of Closed Islands
1261Find Elements in a Contaminated Binary Tree
1267Count Servers that Communicate
1302Deepest Leaves Sum
1305All Elements in Two Binary Search Trees
1306Jump Game III
1315Sum of Nodes with Even-Valued Grandparent
1319Number of Operations to Make Network Connected
1325Delete Leaves With a Given Value
1339Maximum Product of Splitted Binary Tree
1361Validate Binary Tree Nodes
1367Linked List in Binary Tree
1372Longest ZigZag Path in a Binary Tree
1376Time Needed to Inform All Employees
1382Balance a Binary Search Tree
1391Check if There is a Valid Path in a Grid
1443Minimum Time to Collect All Apples in a Tree
1448Count Good Nodes in Binary Tree
1457Pseudo-Palindromic Paths in a Binary Tree
1462Course Schedule IV
1466Reorder Routes to Make All Paths Lead to the City Zero
1519Number of Nodes in the Sub-Tree With the Same Label
1530Number of Good Leaf Nodes Pairs
1559Detect Cycles in 2D Grid
1600Throne Inheritance
1625Lexicographically Smallest String After Applying Operations
1631Path With Minimum Effort
1722Minimize Hamming Distance After Swap Operations
1743Restore the Array From Adjacent Pairs
1905Count Sub Islands
1992Find All Groups of Farmland
1993Operations on Tree
2049Count Nodes With the Highest Score
2096Step-By-Step Directions From a Binary Tree Node to Another
2101Detonate the Maximum Bombs
2192All Ancestors of a Node in a Directed Acyclic Graph
2265Count Nodes Equal to Average of Subtree
2316Count Unreachable Pairs of Nodes in an Undirected Graph
2359Find Closest Node to Given Two Nodes
2368Reachable Nodes With Restrictions
2385Amount of Time for Binary Tree to Be Infected
2415Reverse Odd Levels of Binary Tree
2467Most Profitable Path in a Tree
2476Closest Nodes Queries in a Binary Search Tree
2477Minimum Fuel Cost to Report to the Capital
2492Minimum Score of a Path Between Two Cities

Hard (37)

Hard problems
No.Problem
0124Binary Tree Maximum Path Sum
0297Serialize and Deserialize Binary Tree
0329Longest Increasing Path in a Matrix
0332Reconstruct Itinerary
0472Concatenated Words
0514Freedom Trail
0685Redundant Connection II
0749Contain Virus
0753Cracking the Safe
0765Couples Holding Hands
0778Swim in Rising Water
0827Making A Large Island
0834Sum of Distances in Tree
0839Similar String Groups
0924Minimize Malware Spread
0928Minimize Malware Spread II
0968Binary Tree Cameras
0987Vertical Order Traversal of a Binary Tree
1028Recover a Tree From Preorder Traversal
1036Escape a Large Maze
1192Critical Connections in a Network
1203Sort Items by Groups Respecting Dependencies
1373Maximum Sum BST in Binary Tree
1377Frog Position After T Seconds
1483Kth Ancestor of a Tree Node
1568Minimum Number of Days to Disconnect Island
1766Tree of Coprimes
1970Last Day Where You Can Still Cross
2003Smallest Missing Genetic Value in Each Subtree
2092Find All People With Secret
2127Maximum Employees to Be Invited to a Meeting
2246Longest Path With Different Adjacent Characters
2322Minimum Score After Removals on a Tree
2328Number of Increasing Paths in a Grid
2360Longest Cycle in a Graph
2440Create Components With Same Value
2458Height of Binary Tree After Subtree Removal Queries