method
DFS — animated LeetCode walkthroughs
LeetCode problems solved with DFS. Browse the complete tag, then watch the algorithm move — step-by-step animation, Python and Go.
349 problems · 265 animated · 84 locked
Start with a walkthrough
Full DFS index
- 79Word SearchWatch
- 94Binary Tree Inorder TraversalWatch
- 98Validate Binary Search TreeWatch
- 99Recover Binary Search TreeWatch
- 100Same TreeWatch
- 101Symmetric TreeWatch
- 104Maximum Depth of Binary TreeWatch
- 110Balanced Binary TreeWatch
- 111Minimum Depth of Binary TreeWatch
- 112Path SumWatch
- 113Path Sum IIWatch
- 114Flatten Binary Tree to Linked ListWatch
- 116Populating Next Right Pointers in Each NodeWatch
- 117Populating Next Right Pointers in Each Node IIWatch
- 124Binary Tree Maximum Path SumWatch
- 129Sum Root to Leaf NumbersWatch
- 130Surrounded RegionsWatch
- 133Clone GraphWatch
- 144Binary Tree Preorder TraversalWatch
- 145Binary Tree Postorder TraversalWatch
- 156Binary Tree Upside DownLocked
- 199Binary Tree Right Side ViewWatch
- 200Number of IslandsWatch
- 207Course ScheduleWatch
- 210Course Schedule IIWatch
- 211Design Add and Search Words Data StructureWatch
- 226Invert Binary TreeWatch
- 230Kth Smallest Element in a BSTWatch
- 235Lowest Common Ancestor of a Binary Search TreeWatch
- 236Lowest Common Ancestor of a Binary TreeWatch
- 250Count Univalue SubtreesLocked
- 257Binary Tree PathsWatch
- 261Graph Valid TreeLocked
- 269Alien DictionaryLocked
- 270Closest Binary Search Tree ValueLocked
- 272Closest Binary Search Tree Value IILocked
- 285Inorder Successor in BSTLocked
- 297Serialize and Deserialize Binary TreeWatch
- 298Binary Tree Longest Consecutive SequenceLocked
- 302Smallest Rectangle Enclosing Black PixelsLocked
- 310Minimum Height TreesWatch
- 314Binary Tree Vertical Order TraversalLocked
- 323Number of Connected Components in an Undirected GraphLocked
- 329Longest Increasing Path in a MatrixWatch
- 332Reconstruct ItineraryWatch
- 333Largest BST SubtreeLocked
- 337House Robber IIIWatch
- 339Nested List Weight SumLocked
- 341Flatten Nested List IteratorWatch
- 364Nested List Weight Sum IILocked
- 365Water and Jug ProblemWatch
- 366Find Leaves of Binary TreeLocked
- 385Mini ParserWatch
- 386Lexicographical NumbersWatch
- 388Longest Absolute File PathWatch
- 399Evaluate DivisionWatch
- 404Sum of Left LeavesWatch
- 417Pacific Atlantic Water FlowWatch
- 419Battleships in a BoardWatch
- 426Convert Binary Search Tree to Sorted Doubly Linked ListLocked
- 428Serialize and Deserialize N-ary TreeLocked
- 430Flatten a Multilevel Doubly Linked ListWatch
- 431Encode N-ary Tree to Binary TreeLocked
- 437Path Sum IIIWatch
- 449Serialize and Deserialize BSTWatch
- 463Island PerimeterWatch
- 472Concatenated WordsWatch
- 490The MazeLocked
- 499The Maze IIILocked
- 501Find Mode in Binary Search TreeWatch
- 505The Maze IILocked
- 508Most Frequent Subtree SumWatch
- 513Find Bottom Left Tree ValueWatch
- 514Freedom TrailWatch
- 515Find Largest Value in Each Tree RowWatch
- 529MinesweeperWatch
- 530Minimum Absolute Difference in BSTWatch
- 536Construct Binary Tree from StringLocked
- 538Convert BST to Greater TreeWatch
- 543Diameter of Binary TreeWatch
- 545Boundary of Binary TreeLocked
- 547Number of ProvincesWatch
- 549Binary Tree Longest Consecutive Sequence IILocked
- 559Maximum Depth of N-ary TreeWatch
- 563Binary Tree TiltWatch
- 565Array NestingWatch
- 572Subtree of Another TreeWatch
- 582Kill ProcessLocked
- 589N-ary Tree Preorder TraversalWatch
- 590N-ary Tree Postorder TraversalWatch
- 606Construct String from Binary TreeWatch
- 617Merge Two Binary TreesWatch
- 623Add One Row to TreeWatch
- 637Average of Levels in Binary TreeWatch
- 642Design Search Autocomplete SystemLocked
- 652Find Duplicate SubtreesWatch
- 653Two Sum IV - Input is a BSTWatch
- 655Print Binary TreeWatch
- 662Maximum Width of Binary TreeWatch
- 663Equal Tree PartitionLocked
- 666Path Sum IVLocked
- 669Trim a Binary Search TreeWatch
- 671Second Minimum Node In a Binary TreeWatch
- 672Bulb Switcher IIWatch
- 676Implement Magic DictionaryWatch
- 684Redundant ConnectionWatch
- 685Redundant Connection IIWatch
- 687Longest Univalue PathWatch
- 690Employee ImportanceWatch
- 694Number of Distinct IslandsLocked
- 695Max Area of IslandWatch
- 711Number of Distinct Islands IILocked
- 721Accounts MergeWatch
- 733Flood FillWatch
- 737Sentence Similarity IILocked
- 742Closest Leaf in a Binary TreeLocked
- 743Network Delay TimeWatch
- 749Contain VirusWatch
- 753Cracking the SafeWatch
- 765Couples Holding HandsWatch
- 778Swim in Rising WaterWatch
- 783Minimum Distance Between BST NodesWatch
- 785Is Graph Bipartite?Watch
- 787Cheapest Flights Within K StopsWatch
- 797All Paths From Source to TargetWatch
- 802Find Eventual Safe StatesWatch
- 814Binary Tree PruningWatch
- 827Making A Large IslandWatch
- 834Sum of Distances in TreeWatch
- 839Similar String GroupsWatch
- 841Keys and RoomsWatch
- 851Loud and RichWatch
- 863All Nodes Distance K in Binary TreeWatch
- 865Smallest Subtree with all the Deepest NodesWatch
- 872Leaf-Similar TreesWatch
- 886Possible BipartitionWatch
- 897Increasing Order Search TreeWatch
- 924Minimize Malware SpreadWatch
- 928Minimize Malware Spread IIWatch
- 934Shortest BridgeWatch
- 938Range Sum of BSTWatch
- 947Most Stones Removed with Same Row or ColumnWatch
- 951Flip Equivalent Binary TreesWatch
- 959Regions Cut By SlashesWatch
- 965Univalued Binary TreeWatch
- 968Binary Tree CamerasWatch
- 971Flip Binary Tree To Match Preorder TraversalWatch
- 979Distribute Coins in Binary TreeWatch
- 987Vertical Order Traversal of a Binary TreeWatch
- 988Smallest String Starting From LeafWatch
- 993Cousins in Binary TreeWatch
- 1020Number of EnclavesWatch
- 1022Sum of Root To Leaf Binary NumbersWatch
- 1026Maximum Difference Between Node and AncestorWatch
- 1028Recover a Tree From Preorder TraversalWatch
- 1034Coloring A BorderWatch
- 1036Escape a Large MazeWatch
- 1038Binary Search Tree to Greater Sum TreeWatch
- 1042Flower Planting With No AdjacentWatch
- 1080Insufficient Nodes in Root to Leaf PathsWatch
- 1102Path With Maximum Minimum ValueLocked
- 1110Delete Nodes And Return ForestWatch
- 1120Maximum Average SubtreeLocked
- 1123Lowest Common Ancestor of Deepest LeavesWatch
- 1145Binary Tree Coloring GameWatch
- 1161Maximum Level Sum of a Binary TreeWatch
- 1192Critical Connections in a NetworkWatch
- 1202Smallest String With SwapsWatch
- 1203Sort Items by Groups Respecting DependenciesWatch
- 1214Two Sum BSTsLocked
- 1233Remove Sub-Folders from the FilesystemWatch
- 1236Web CrawlerLocked
- 1242Web Crawler MultithreadedLocked
- 1245Tree DiameterLocked
- 1254Number of Closed IslandsWatch
- 1257Smallest Common RegionLocked
- 1261Find Elements in a Contaminated Binary TreeWatch
- 1267Count Servers that CommunicateWatch
- 1273Delete Tree NodesLocked
- 1302Deepest Leaves SumWatch
- 1305All Elements in Two Binary Search TreesWatch
- 1306Jump Game IIIWatch
- 1315Sum of Nodes with Even-Valued GrandparentWatch
- 1319Number of Operations to Make Network ConnectedWatch
- 1325Delete Leaves With a Given ValueWatch
- 1339Maximum Product of Splitted Binary TreeWatch
- 1361Validate Binary Tree NodesWatch
- 1367Linked List in Binary TreeWatch
- 1372Longest ZigZag Path in a Binary TreeWatch
- 1373Maximum Sum BST in Binary TreeWatch
- 1376Time Needed to Inform All EmployeesWatch
- 1377Frog Position After T SecondsWatch
- 1379Find a Corresponding Node of a Binary Tree in a Clone of That TreeWatch
- 1382Balance a Binary Search TreeWatch
- 1391Check if There is a Valid Path in a GridWatch
- 1430Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary TreeLocked
- 1443Minimum Time to Collect All Apples in a TreeWatch
- 1448Count Good Nodes in Binary TreeWatch
- 1457Pseudo-Palindromic Paths in a Binary TreeWatch
- 1462Course Schedule IVWatch
- 1466Reorder Routes to Make All Paths Lead to the City ZeroWatch
- 1469Find All The Lonely NodesLocked
- 1483Kth Ancestor of a Tree NodeWatch
- 1485Clone Binary Tree With Random PointerLocked
- 1490Clone N-ary TreeLocked
- 1506Find Root of N-Ary TreeLocked
- 1516Move Sub-Tree of N-Ary TreeLocked
- 1519Number of Nodes in the Sub-Tree With the Same LabelWatch
- 1522Diameter of N-Ary TreeLocked
- 1530Number of Good Leaf Nodes PairsWatch
- 1559Detect Cycles in 2D GridWatch
- 1568Minimum Number of Days to Disconnect IslandWatch
- 1600Throne InheritanceWatch
- 1612Check If Two Expression Trees are EquivalentLocked
- 1625Lexicographically Smallest String After Applying OperationsWatch
- 1631Path With Minimum EffortWatch
- 1644Lowest Common Ancestor of a Binary Tree IILocked
- 1660Correct a Binary TreeLocked
- 1666Change the Root of a Binary TreeLocked
- 1676Lowest Common Ancestor of a Binary Tree IVLocked
- 1722Minimize Hamming Distance After Swap OperationsWatch
- 1724Checking Existence of Edge Length Limited Paths IILocked
- 1740Find Distance in a Binary TreeLocked
- 1743Restore the Array From Adjacent PairsWatch
- 1766Tree of CoprimesWatch
- 1778Shortest Path in a Hidden GridLocked
- 1810Minimum Path Cost in a Hidden GridLocked
- 1820Maximum Number of Accepted InvitationsLocked
- 1858Longest Word With All PrefixesLocked
- 1905Count Sub IslandsWatch
- 1916Count Ways to Build Rooms in an Ant ColonyWatch
- 1932Merge BSTs to Create Single BSTWatch
- 1938Maximum Genetic Difference QueryWatch
- 1948Delete Duplicate Folders in SystemWatch
- 1970Last Day Where You Can Still CrossWatch
- 1971Find if Path Exists in GraphWatch
- 1973Count Nodes Equal to Sum of DescendantsLocked
- 1992Find All Groups of FarmlandWatch
- 1993Operations on TreeWatch
- 2003Smallest Missing Genetic Value in Each SubtreeWatch
- 2049Count Nodes With the Highest ScoreWatch
- 2092Find All People With SecretWatch
- 2096Step-By-Step Directions From a Binary Tree Node to AnotherWatch
- 2097Valid Arrangement of PairsWatch
- 2101Detonate the Maximum BombsWatch
- 2123Minimum Operations to Remove Adjacent Ones in MatrixLocked
- 2127Maximum Employees to Be Invited to a MeetingWatch
- 2192All Ancestors of a Node in a Directed Acyclic GraphWatch
- 2204Distance to a Cycle in Undirected GraphLocked
- 2246Longest Path With Different Adjacent CharactersWatch
- 2265Count Nodes Equal to Average of SubtreeWatch
- 2277Closest Node to Path in TreeLocked
- 2307Check for Contradictions in EquationsLocked
- 2313Minimum Flips in Binary Tree to Get ResultLocked
- 2316Count Unreachable Pairs of Nodes in an Undirected GraphWatch
- 2322Minimum Score After Removals on a TreeWatch
- 2328Number of Increasing Paths in a GridWatch
- 2331Evaluate Boolean Binary TreeWatch
- 2359Find Closest Node to Given Two NodesWatch
- 2360Longest Cycle in a GraphWatch
- 2368Reachable Nodes With RestrictionsWatch
- 2378Choose Edges to Maximize Score in a TreeLocked
- 2385Amount of Time for Binary Tree to Be InfectedWatch
- 2415Reverse Odd Levels of Binary TreeWatch
- 2440Create Components With Same ValueWatch
- 2445Number of Nodes With Value OneLocked
- 2458Height of Binary Tree After Subtree Removal QueriesWatch
- 2467Most Profitable Path in a TreeWatch
- 2476Closest Nodes Queries in a Binary Search TreeWatch
- 2477Minimum Fuel Cost to Report to the CapitalWatch
- 2479Maximum XOR of Two Non-Overlapping SubtreesLocked
- 2492Minimum Score of a Path Between Two CitiesWatch
- 2493Divide Nodes Into the Maximum Number of GroupsWatch
- 2538Difference Between Maximum and Minimum Price SumWatch
- 2556Disconnect Path in a Binary Matrix by at Most One FlipWatch
- 2581Count Number of Possible Root NodesWatch
- 2596Check Knight Tour ConfigurationWatch
- 2641Cousins in Binary Tree IIWatch
- 2646Minimize the Total Price of the TripsWatch
- 2658Maximum Number of Fish in a GridWatch
- 2685Count the Number of Complete ComponentsWatch
- 2689Extract Kth Character From The Rope TreeLocked
- 2764Is Array a Preorder of Some Binary TreeLocked
- 2773Height of Special Binary TreeLocked
- 2791Count Paths That Can Form a Palindrome in a TreeWatch
- 2792Count Nodes That Are Great EnoughLocked
- 2846Minimum Edge Weight Equilibrium Queries in a TreeWatch
- 2852Sum of Remoteness of All CellsLocked
- 2858Minimum Edge Reversals So Every Node Is ReachableWatch
- 2867Count Valid Paths in a TreeWatch
- 2872Maximum Number of K-Divisible ComponentsWatch
- 2876Count Visited Nodes in a Directed GraphWatch
- 2920Maximum Points After Collecting Coins From All NodesWatch
- 2925Maximum Score After Applying Operations on a TreeWatch
- 2973Find Number of Coins to Place in Tree NodesWatch
- 3004Maximum Subtree of the Same ColorLocked
- 3067Count Pairs of Connectable Servers in a Weighted Tree NetworkWatch
- 3123Find Edges in Shortest PathsWatch
- 3157Find the Level of Tree with Minimum SumLocked
- 3203Find Minimum Diameter After Merging Two TreesWatch
- 3235Check if the Rectangle Corner Is ReachableWatch
- 3241Time Taken to Mark All NodesWatch
- 3249Count the Number of Good NodesWatch
- 3310Remove Methods From ProjectWatch
- 3313Find the Last Marked Nodes in TreeLocked
- 3319K-th Largest Perfect Subtree Size in Binary TreeWatch
- 3327Check if DFS Strings Are PalindromesWatch
- 3331Find Subtree Sizes After ChangesWatch
- 3367Maximize Sum of Weights after Edge RemovalsWatch
- 3372Maximize the Number of Target Nodes After Connecting Trees IWatch
- 3373Maximize the Number of Target Nodes After Connecting Trees IIWatch
- 3383Minimum Runes to Add to Cast SpellLocked
- 3387Maximize Amount After Two Days of ConversionsWatch
- 3419Minimize the Maximum Edge Weight of GraphWatch
- 3425Longest Special PathWatch
- 3481Apply SubstitutionsLocked
- 3486Longest Special Path IIWatch
- 3493Properties GraphWatch
- 3515Shortest Path in a Weighted TreeWatch
- 3528Unit Conversion IWatch
- 3535Unit Conversion IILocked
- 3544Subtree Inversion SumWatch
- 3553Minimum Weighted Subgraph With the Required Paths IIWatch
- 3558Number of Ways to Assign Edge Weights IWatch
- 3559Number of Ways to Assign Edge Weights IIWatch
- 3562Maximum Profit from Trading Stocks with DiscountsWatch
- 3575Maximum Good Subtree ScoreWatch
- 3585Find Weighted Median Node in TreeWatch
- 3590Kth Smallest Path XOR SumWatch
- 3593Minimum Increments to Equalize Leaf PathsWatch
- 3607Power Grid MaintenanceWatch
- 3619Count Islands With Total Value Divisible by KWatch
- 3710Maximum Partition FactorWatch
- 3715Sum of Perfect Square AncestorsWatch
- 3772Maximum Subgraph Score in a TreeWatch
- 3786Total Sum of Interaction Cost in Tree GroupsWatch
- 3812Minimum Edge Toggles on a TreeWatch
- 3831Median of a Binary Search Tree LevelLocked
- 3841Palindromic Path Queries in a TreeWatch
- 3879Maximum Distinct Path Sum in a Binary TreeLocked
- 3910Count Connected Subgraphs with Even Node SumWatch
- 3939Count Non Adjacent Subsets in a Rooted TreeWatch
- 3949Subtree Inversion Sum IILocked
- 3965Finish Time of Tasks IWatch
- 3967Finish Time of Tasks IILocked
- 3973Distinct Gate Paths to LCALocked
- 3997Count Dominant Nodes in a Binary TreeWatch
- 4015Weighted Sum of a TreeWatch
- 4018Total Sum of Interaction Cost in Tree Groups IILocked