Depth-First Search
77 problems
DFS explores as far as possible before backtracking. Use it for path finding, cycle detection, connected components, and tree traversal. Implement recursively (implicit call stack) or iteratively with an explicit Stack<T>. Mark visited nodes to avoid infinite loops in graphs.
- 99.Recover Binary Search TreeMedium
- 100.Same TreeEasy
- 110.Balanced Binary TreeEasy
- 111.Minimum Depth of Binary TreeEasy
- 112.Path SumEasy
- 113.Path Sum IIMedium
- 114.Flatten Binary Tree to Linked ListMedium
- 144.Binary Tree Preorder TraversalEasy
- 145.Binary Tree Postorder TraversalEasy
- 210.Course Schedule IIMedium
- 222.Count Complete Tree NodesMedium
- 230.Kth Smallest Element in a BSTMedium
- 236.Lowest Common Ancestor of a Binary TreeMedium
- 257.Binary Tree PathsEasy
- 310.Minimum Height TreesUnknown
- 329.Longest Increasing Path in a MatrixHard
- 386.Lexicographical NumbersMedium
- 417.Pacific Atlantic Water FlowMedium
- 508.Most Frequent Subtree SumMedium
- 513.Find Bottom Left Tree ValueMedium
- 515.Find Largest Value in Each Tree RowMedium
- 590.N-ary Tree Postorder TraversalEasy
- 684.Redundant ConnectionMedium
- 778.Swim in Rising WaterHard
- 802.Find Eventual Safe StatesMedium
- 827.Making A Large IslandHard
- 834.Sum of Distances in TreeUnknown
- 839.Similar String GroupsHard
- 851.Loud and RichMedium
- 865.Smallest Subtree with all the Deepest NodesMedium
- 928.Minimize Malware Spread IIUnknown
- 947.Most Stones Removed with Same Row or ColumnMedium
- 951.Flip Equivalent Binary TreesMedium
- 959.Regions Cut By SlashesUnknown
- 965.Univalued Binary TreeEasy
- 1022.Sum of Root To Leaf Binary NumbersEasy
- 1028.Recover a Tree From Preorder TraversalHard
- 1038.Binary Search Tree to Greater Sum TreeMedium
- 1080.Insufficient Nodes in Root to Leaf PathsMedium
- 1110.Delete Nodes And Return ForestMedium
- 1123.Lowest Common Ancestor of Deepest LeavesMedium
- 1161.Maximum Level Sum of a Binary TreeMedium
- 1233.Remove Sub-Folders from the FilesystemMedium
- 1261.Find Elements in a Contaminated Binary TreeMedium
- 1305.All Elements in Two Binary Search TreesUnknown
- 1319.Number of Operations to Make Network ConnectedMedium
- 1339.Maximum Product of Splitted Binary TreeMedium
- 1367.Linked List in Binary TreeMedium
- 1462.Course Schedule IVMedium
- 1530.Number of Good Leaf Nodes PairsMedium
- 1568.Minimum Number of Days to Disconnect IslandUnknown
- 1766.Tree of CoprimesUnknown
- 1905.Count Sub IslandsUnknown
- 1938.Maximum Genetic Difference QueryUnknown
- 1948.Delete Duplicate Folders in SystemUnknown
- 1970.Last Day Where You Can Still CrossUnknown
- 1971.Find if Path Exists in GraphEasy
- 2092.Find All People With SecretUnknown
- 2096.Step-By-Step Directions From a Binary Tree Node to AnotherMedium
- 2097.Valid Arrangement of PairsUnknown
- 2127.Maximum Employees to Be Invited to a MeetingUnknown
- 2192.All Ancestors of a Node in a Directed Acyclic GraphMedium
- 2322.Minimum Score After Removals on a TreeUnknown
- 2331.Evaluate Boolean Binary TreeEasy
- 2415.Reverse Odd Levels of Binary TreeMedium
- 2440.Create Components With Same ValueMedium
- 2458.Height of Binary Tree After Subtree Removal QueriesUnknown
- 2467.Most Profitable Path in a TreeMedium
- 2641.Cousins in Binary Tree IIMedium
- 2658.Maximum Number of Fish in a GridMedium
- 2685.Count the Number of Complete ComponentsMedium
- 2872.Maximum Number of K-Divisible ComponentsUnknown
- 3068.Find the Maximum Sum of Node ValuesHard
- 3203.Find Minimum Diameter After Merging Two TreesMedium
- 3310.Remove Methods From ProjectUnknown
- 3372.Maximize the Number of Target Nodes After Connecting Trees IMedium
- 3373.Maximize the Number of Target Nodes After Connecting Trees IIUnknown