Skip to content
LEETFLIX

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

  1. 79Word SearchWatch
  2. 94Binary Tree Inorder TraversalWatch
  3. 98Validate Binary Search TreeWatch
  4. 99Recover Binary Search TreeWatch
  5. 100Same TreeWatch
  6. 101Symmetric TreeWatch
  7. 104Maximum Depth of Binary TreeWatch
  8. 110Balanced Binary TreeWatch
  9. 111Minimum Depth of Binary TreeWatch
  10. 112Path SumWatch
  11. 113Path Sum IIWatch
  12. 114Flatten Binary Tree to Linked ListWatch
  13. 116Populating Next Right Pointers in Each NodeWatch
  14. 117Populating Next Right Pointers in Each Node IIWatch
  15. 124Binary Tree Maximum Path SumWatch
  16. 129Sum Root to Leaf NumbersWatch
  17. 130Surrounded RegionsWatch
  18. 133Clone GraphWatch
  19. 144Binary Tree Preorder TraversalWatch
  20. 145Binary Tree Postorder TraversalWatch
  21. 156Binary Tree Upside DownLocked
  22. 199Binary Tree Right Side ViewWatch
  23. 200Number of IslandsWatch
  24. 207Course ScheduleWatch
  25. 210Course Schedule IIWatch
  26. 211Design Add and Search Words Data StructureWatch
  27. 226Invert Binary TreeWatch
  28. 230Kth Smallest Element in a BSTWatch
  29. 235Lowest Common Ancestor of a Binary Search TreeWatch
  30. 236Lowest Common Ancestor of a Binary TreeWatch
  31. 250Count Univalue SubtreesLocked
  32. 257Binary Tree PathsWatch
  33. 261Graph Valid TreeLocked
  34. 269Alien DictionaryLocked
  35. 270Closest Binary Search Tree ValueLocked
  36. 272Closest Binary Search Tree Value IILocked
  37. 285Inorder Successor in BSTLocked
  38. 297Serialize and Deserialize Binary TreeWatch
  39. 298Binary Tree Longest Consecutive SequenceLocked
  40. 302Smallest Rectangle Enclosing Black PixelsLocked
  41. 310Minimum Height TreesWatch
  42. 314Binary Tree Vertical Order TraversalLocked
  43. 323Number of Connected Components in an Undirected GraphLocked
  44. 329Longest Increasing Path in a MatrixWatch
  45. 332Reconstruct ItineraryWatch
  46. 333Largest BST SubtreeLocked
  47. 337House Robber IIIWatch
  48. 339Nested List Weight SumLocked
  49. 341Flatten Nested List IteratorWatch
  50. 364Nested List Weight Sum IILocked
  51. 365Water and Jug ProblemWatch
  52. 366Find Leaves of Binary TreeLocked
  53. 385Mini ParserWatch
  54. 386Lexicographical NumbersWatch
  55. 388Longest Absolute File PathWatch
  56. 399Evaluate DivisionWatch
  57. 404Sum of Left LeavesWatch
  58. 417Pacific Atlantic Water FlowWatch
  59. 419Battleships in a BoardWatch
  60. 426Convert Binary Search Tree to Sorted Doubly Linked ListLocked
  61. 428Serialize and Deserialize N-ary TreeLocked
  62. 430Flatten a Multilevel Doubly Linked ListWatch
  63. 431Encode N-ary Tree to Binary TreeLocked
  64. 437Path Sum IIIWatch
  65. 449Serialize and Deserialize BSTWatch
  66. 463Island PerimeterWatch
  67. 472Concatenated WordsWatch
  68. 490The MazeLocked
  69. 499The Maze IIILocked
  70. 501Find Mode in Binary Search TreeWatch
  71. 505The Maze IILocked
  72. 508Most Frequent Subtree SumWatch
  73. 513Find Bottom Left Tree ValueWatch
  74. 514Freedom TrailWatch
  75. 515Find Largest Value in Each Tree RowWatch
  76. 529MinesweeperWatch
  77. 530Minimum Absolute Difference in BSTWatch
  78. 536Construct Binary Tree from StringLocked
  79. 538Convert BST to Greater TreeWatch
  80. 543Diameter of Binary TreeWatch
  81. 545Boundary of Binary TreeLocked
  82. 547Number of ProvincesWatch
  83. 549Binary Tree Longest Consecutive Sequence IILocked
  84. 559Maximum Depth of N-ary TreeWatch
  85. 563Binary Tree TiltWatch
  86. 565Array NestingWatch
  87. 572Subtree of Another TreeWatch
  88. 582Kill ProcessLocked
  89. 589N-ary Tree Preorder TraversalWatch
  90. 590N-ary Tree Postorder TraversalWatch
  91. 606Construct String from Binary TreeWatch
  92. 617Merge Two Binary TreesWatch
  93. 623Add One Row to TreeWatch
  94. 637Average of Levels in Binary TreeWatch
  95. 642Design Search Autocomplete SystemLocked
  96. 652Find Duplicate SubtreesWatch
  97. 653Two Sum IV - Input is a BSTWatch
  98. 655Print Binary TreeWatch
  99. 662Maximum Width of Binary TreeWatch
  100. 663Equal Tree PartitionLocked
  101. 666Path Sum IVLocked
  102. 669Trim a Binary Search TreeWatch
  103. 671Second Minimum Node In a Binary TreeWatch
  104. 672Bulb Switcher IIWatch
  105. 676Implement Magic DictionaryWatch
  106. 684Redundant ConnectionWatch
  107. 685Redundant Connection IIWatch
  108. 687Longest Univalue PathWatch
  109. 690Employee ImportanceWatch
  110. 694Number of Distinct IslandsLocked
  111. 695Max Area of IslandWatch
  112. 711Number of Distinct Islands IILocked
  113. 721Accounts MergeWatch
  114. 733Flood FillWatch
  115. 737Sentence Similarity IILocked
  116. 742Closest Leaf in a Binary TreeLocked
  117. 743Network Delay TimeWatch
  118. 749Contain VirusWatch
  119. 753Cracking the SafeWatch
  120. 765Couples Holding HandsWatch
  121. 778Swim in Rising WaterWatch
  122. 783Minimum Distance Between BST NodesWatch
  123. 785Is Graph Bipartite?Watch
  124. 787Cheapest Flights Within K StopsWatch
  125. 797All Paths From Source to TargetWatch
  126. 802Find Eventual Safe StatesWatch
  127. 814Binary Tree PruningWatch
  128. 827Making A Large IslandWatch
  129. 834Sum of Distances in TreeWatch
  130. 839Similar String GroupsWatch
  131. 841Keys and RoomsWatch
  132. 851Loud and RichWatch
  133. 863All Nodes Distance K in Binary TreeWatch
  134. 865Smallest Subtree with all the Deepest NodesWatch
  135. 872Leaf-Similar TreesWatch
  136. 886Possible BipartitionWatch
  137. 897Increasing Order Search TreeWatch
  138. 924Minimize Malware SpreadWatch
  139. 928Minimize Malware Spread IIWatch
  140. 934Shortest BridgeWatch
  141. 938Range Sum of BSTWatch
  142. 947Most Stones Removed with Same Row or ColumnWatch
  143. 951Flip Equivalent Binary TreesWatch
  144. 959Regions Cut By SlashesWatch
  145. 965Univalued Binary TreeWatch
  146. 968Binary Tree CamerasWatch
  147. 971Flip Binary Tree To Match Preorder TraversalWatch
  148. 979Distribute Coins in Binary TreeWatch
  149. 987Vertical Order Traversal of a Binary TreeWatch
  150. 988Smallest String Starting From LeafWatch
  151. 993Cousins in Binary TreeWatch
  152. 1020Number of EnclavesWatch
  153. 1022Sum of Root To Leaf Binary NumbersWatch
  154. 1026Maximum Difference Between Node and AncestorWatch
  155. 1028Recover a Tree From Preorder TraversalWatch
  156. 1034Coloring A BorderWatch
  157. 1036Escape a Large MazeWatch
  158. 1038Binary Search Tree to Greater Sum TreeWatch
  159. 1042Flower Planting With No AdjacentWatch
  160. 1080Insufficient Nodes in Root to Leaf PathsWatch
  161. 1102Path With Maximum Minimum ValueLocked
  162. 1110Delete Nodes And Return ForestWatch
  163. 1120Maximum Average SubtreeLocked
  164. 1123Lowest Common Ancestor of Deepest LeavesWatch
  165. 1145Binary Tree Coloring GameWatch
  166. 1161Maximum Level Sum of a Binary TreeWatch
  167. 1192Critical Connections in a NetworkWatch
  168. 1202Smallest String With SwapsWatch
  169. 1203Sort Items by Groups Respecting DependenciesWatch
  170. 1214Two Sum BSTsLocked
  171. 1233Remove Sub-Folders from the FilesystemWatch
  172. 1236Web CrawlerLocked
  173. 1242Web Crawler MultithreadedLocked
  174. 1245Tree DiameterLocked
  175. 1254Number of Closed IslandsWatch
  176. 1257Smallest Common RegionLocked
  177. 1261Find Elements in a Contaminated Binary TreeWatch
  178. 1267Count Servers that CommunicateWatch
  179. 1273Delete Tree NodesLocked
  180. 1302Deepest Leaves SumWatch
  181. 1305All Elements in Two Binary Search TreesWatch
  182. 1306Jump Game IIIWatch
  183. 1315Sum of Nodes with Even-Valued GrandparentWatch
  184. 1319Number of Operations to Make Network ConnectedWatch
  185. 1325Delete Leaves With a Given ValueWatch
  186. 1339Maximum Product of Splitted Binary TreeWatch
  187. 1361Validate Binary Tree NodesWatch
  188. 1367Linked List in Binary TreeWatch
  189. 1372Longest ZigZag Path in a Binary TreeWatch
  190. 1373Maximum Sum BST in Binary TreeWatch
  191. 1376Time Needed to Inform All EmployeesWatch
  192. 1377Frog Position After T SecondsWatch
  193. 1379Find a Corresponding Node of a Binary Tree in a Clone of That TreeWatch
  194. 1382Balance a Binary Search TreeWatch
  195. 1391Check if There is a Valid Path in a GridWatch
  196. 1430Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary TreeLocked
  197. 1443Minimum Time to Collect All Apples in a TreeWatch
  198. 1448Count Good Nodes in Binary TreeWatch
  199. 1457Pseudo-Palindromic Paths in a Binary TreeWatch
  200. 1462Course Schedule IVWatch
  201. 1466Reorder Routes to Make All Paths Lead to the City ZeroWatch
  202. 1469Find All The Lonely NodesLocked
  203. 1483Kth Ancestor of a Tree NodeWatch
  204. 1485Clone Binary Tree With Random PointerLocked
  205. 1490Clone N-ary TreeLocked
  206. 1506Find Root of N-Ary TreeLocked
  207. 1516Move Sub-Tree of N-Ary TreeLocked
  208. 1519Number of Nodes in the Sub-Tree With the Same LabelWatch
  209. 1522Diameter of N-Ary TreeLocked
  210. 1530Number of Good Leaf Nodes PairsWatch
  211. 1559Detect Cycles in 2D GridWatch
  212. 1568Minimum Number of Days to Disconnect IslandWatch
  213. 1600Throne InheritanceWatch
  214. 1612Check If Two Expression Trees are EquivalentLocked
  215. 1625Lexicographically Smallest String After Applying OperationsWatch
  216. 1631Path With Minimum EffortWatch
  217. 1644Lowest Common Ancestor of a Binary Tree IILocked
  218. 1660Correct a Binary TreeLocked
  219. 1666Change the Root of a Binary TreeLocked
  220. 1676Lowest Common Ancestor of a Binary Tree IVLocked
  221. 1722Minimize Hamming Distance After Swap OperationsWatch
  222. 1724Checking Existence of Edge Length Limited Paths IILocked
  223. 1740Find Distance in a Binary TreeLocked
  224. 1743Restore the Array From Adjacent PairsWatch
  225. 1766Tree of CoprimesWatch
  226. 1778Shortest Path in a Hidden GridLocked
  227. 1810Minimum Path Cost in a Hidden GridLocked
  228. 1820Maximum Number of Accepted InvitationsLocked
  229. 1858Longest Word With All PrefixesLocked
  230. 1905Count Sub IslandsWatch
  231. 1916Count Ways to Build Rooms in an Ant ColonyWatch
  232. 1932Merge BSTs to Create Single BSTWatch
  233. 1938Maximum Genetic Difference QueryWatch
  234. 1948Delete Duplicate Folders in SystemWatch
  235. 1970Last Day Where You Can Still CrossWatch
  236. 1971Find if Path Exists in GraphWatch
  237. 1973Count Nodes Equal to Sum of DescendantsLocked
  238. 1992Find All Groups of FarmlandWatch
  239. 1993Operations on TreeWatch
  240. 2003Smallest Missing Genetic Value in Each SubtreeWatch
  241. 2049Count Nodes With the Highest ScoreWatch
  242. 2092Find All People With SecretWatch
  243. 2096Step-By-Step Directions From a Binary Tree Node to AnotherWatch
  244. 2097Valid Arrangement of PairsWatch
  245. 2101Detonate the Maximum BombsWatch
  246. 2123Minimum Operations to Remove Adjacent Ones in MatrixLocked
  247. 2127Maximum Employees to Be Invited to a MeetingWatch
  248. 2192All Ancestors of a Node in a Directed Acyclic GraphWatch
  249. 2204Distance to a Cycle in Undirected GraphLocked
  250. 2246Longest Path With Different Adjacent CharactersWatch
  251. 2265Count Nodes Equal to Average of SubtreeWatch
  252. 2277Closest Node to Path in TreeLocked
  253. 2307Check for Contradictions in EquationsLocked
  254. 2313Minimum Flips in Binary Tree to Get ResultLocked
  255. 2316Count Unreachable Pairs of Nodes in an Undirected GraphWatch
  256. 2322Minimum Score After Removals on a TreeWatch
  257. 2328Number of Increasing Paths in a GridWatch
  258. 2331Evaluate Boolean Binary TreeWatch
  259. 2359Find Closest Node to Given Two NodesWatch
  260. 2360Longest Cycle in a GraphWatch
  261. 2368Reachable Nodes With RestrictionsWatch
  262. 2378Choose Edges to Maximize Score in a TreeLocked
  263. 2385Amount of Time for Binary Tree to Be InfectedWatch
  264. 2415Reverse Odd Levels of Binary TreeWatch
  265. 2440Create Components With Same ValueWatch
  266. 2445Number of Nodes With Value OneLocked
  267. 2458Height of Binary Tree After Subtree Removal QueriesWatch
  268. 2467Most Profitable Path in a TreeWatch
  269. 2476Closest Nodes Queries in a Binary Search TreeWatch
  270. 2477Minimum Fuel Cost to Report to the CapitalWatch
  271. 2479Maximum XOR of Two Non-Overlapping SubtreesLocked
  272. 2492Minimum Score of a Path Between Two CitiesWatch
  273. 2493Divide Nodes Into the Maximum Number of GroupsWatch
  274. 2538Difference Between Maximum and Minimum Price SumWatch
  275. 2556Disconnect Path in a Binary Matrix by at Most One FlipWatch
  276. 2581Count Number of Possible Root NodesWatch
  277. 2596Check Knight Tour ConfigurationWatch
  278. 2641Cousins in Binary Tree IIWatch
  279. 2646Minimize the Total Price of the TripsWatch
  280. 2658Maximum Number of Fish in a GridWatch
  281. 2685Count the Number of Complete ComponentsWatch
  282. 2689Extract Kth Character From The Rope TreeLocked
  283. 2764Is Array a Preorder of Some ‌Binary TreeLocked
  284. 2773Height of Special Binary TreeLocked
  285. 2791Count Paths That Can Form a Palindrome in a TreeWatch
  286. 2792Count Nodes That Are Great EnoughLocked
  287. 2846Minimum Edge Weight Equilibrium Queries in a TreeWatch
  288. 2852Sum of Remoteness of All CellsLocked
  289. 2858Minimum Edge Reversals So Every Node Is ReachableWatch
  290. 2867Count Valid Paths in a TreeWatch
  291. 2872Maximum Number of K-Divisible ComponentsWatch
  292. 2876Count Visited Nodes in a Directed GraphWatch
  293. 2920Maximum Points After Collecting Coins From All NodesWatch
  294. 2925Maximum Score After Applying Operations on a TreeWatch
  295. 2973Find Number of Coins to Place in Tree NodesWatch
  296. 3004Maximum Subtree of the Same ColorLocked
  297. 3067Count Pairs of Connectable Servers in a Weighted Tree NetworkWatch
  298. 3123Find Edges in Shortest PathsWatch
  299. 3157Find the Level of Tree with Minimum SumLocked
  300. 3203Find Minimum Diameter After Merging Two TreesWatch
  301. 3235Check if the Rectangle Corner Is ReachableWatch
  302. 3241Time Taken to Mark All NodesWatch
  303. 3249Count the Number of Good NodesWatch
  304. 3310Remove Methods From ProjectWatch
  305. 3313Find the Last Marked Nodes in TreeLocked
  306. 3319K-th Largest Perfect Subtree Size in Binary TreeWatch
  307. 3327Check if DFS Strings Are PalindromesWatch
  308. 3331Find Subtree Sizes After ChangesWatch
  309. 3367Maximize Sum of Weights after Edge RemovalsWatch
  310. 3372Maximize the Number of Target Nodes After Connecting Trees IWatch
  311. 3373Maximize the Number of Target Nodes After Connecting Trees IIWatch
  312. 3383Minimum Runes to Add to Cast SpellLocked
  313. 3387Maximize Amount After Two Days of ConversionsWatch
  314. 3419Minimize the Maximum Edge Weight of GraphWatch
  315. 3425Longest Special PathWatch
  316. 3481Apply SubstitutionsLocked
  317. 3486Longest Special Path IIWatch
  318. 3493Properties GraphWatch
  319. 3515Shortest Path in a Weighted TreeWatch
  320. 3528Unit Conversion IWatch
  321. 3535Unit Conversion IILocked
  322. 3544Subtree Inversion SumWatch
  323. 3553Minimum Weighted Subgraph With the Required Paths IIWatch
  324. 3558Number of Ways to Assign Edge Weights IWatch
  325. 3559Number of Ways to Assign Edge Weights IIWatch
  326. 3562Maximum Profit from Trading Stocks with DiscountsWatch
  327. 3575Maximum Good Subtree ScoreWatch
  328. 3585Find Weighted Median Node in TreeWatch
  329. 3590Kth Smallest Path XOR SumWatch
  330. 3593Minimum Increments to Equalize Leaf PathsWatch
  331. 3607Power Grid MaintenanceWatch
  332. 3619Count Islands With Total Value Divisible by KWatch
  333. 3710Maximum Partition FactorWatch
  334. 3715Sum of Perfect Square AncestorsWatch
  335. 3772Maximum Subgraph Score in a TreeWatch
  336. 3786Total Sum of Interaction Cost in Tree GroupsWatch
  337. 3812Minimum Edge Toggles on a TreeWatch
  338. 3831Median of a Binary Search Tree LevelLocked
  339. 3841Palindromic Path Queries in a TreeWatch
  340. 3879Maximum Distinct Path Sum in a Binary TreeLocked
  341. 3910Count Connected Subgraphs with Even Node SumWatch
  342. 3939Count Non Adjacent Subsets in a Rooted TreeWatch
  343. 3949Subtree Inversion Sum IILocked
  344. 3965Finish Time of Tasks IWatch
  345. 3967Finish Time of Tasks IILocked
  346. 3973Distinct Gate Paths to LCALocked
  347. 3997Count Dominant Nodes in a Binary TreeWatch
  348. 4015Weighted Sum of a TreeWatch
  349. 4018Total Sum of Interaction Cost in Tree Groups IILocked