↓Skip to main content

Dfs

Tree Traversal and Common Techniques
·11 mins
Algorithm Tree Binary-Tree Dfs Bfs Bst
DFS return values, BFS level order, BST bounds, climbing to the LCA, tree diameter, and leaf trimming
BFS and Graph Traversal
·6 mins
Algorithm Bfs Dfs Graph Matrix Union-Find
Connected components, grid flood fill, multi-source BFS, state-space BFS, shortest cycles, and recursion depth in DFS
Backtracking
·6 mins
Algorithm Backtracking Recursion Dfs Pruning
The choose, recurse, undo template for subsets, permutations, constrained placement, grid search, and pruning
LeetCode 827: Making A Large Island
·3 mins
LeetCode Hard Dfs Bfs Matrix Union-Find
LeetCode 827 note: label every island and record its size, then for each 0 add the sizes of the distinct neighboring islands plus 1
LeetCode 3203: Find Minimum Diameter After Merging Two Trees
·3 mins
LeetCode Hard Tree Bfs Dfs Graph Tree-Diameter
LeetCode 3203 note: connect the two centers; the answer is max(d1, d2, ceil(d1/2) + ceil(d2/2) + 1)
LeetCode 1028: Recover a Tree From Preorder Traversal
·3 mins
LeetCode Hard Tree Binary-Tree Dfs Stack String
LeetCode 1028 note: a stack holds the path from the root to the current node, so its length is the depth
LeetCode 323: Number of Connected Components in an Undirected Graph
·2 mins
LeetCode Medium Graph Dfs Bfs Union-Find
LeetCode 323 note: count components with DFS or Union-Find; Union-Find without path compression is much slower
LeetCode 1377: Frog Position After T Seconds
·3 mins
LeetCode Hard Tree Dfs Bfs Graph Probability
LeetCode 1377 note: undirected edges, seen must hold nodes, and an early arrival depends on whether the frog can still move
LeetCode 980: Unique Paths III
·2 mins
LeetCode Hard Backtracking Matrix Dfs
LeetCode 980 note: grid backtracking that marks visited cells in place and checks at the end that every empty cell was covered
LeetCode 865: Smallest Subtree with all the Deepest Nodes
·2 mins
LeetCode Medium Tree Binary-Tree Bfs Dfs Lca
LeetCode 865 note: BFS to the last level and climb together, or one DFS returning (depth, node)
LeetCode 1339: Maximum Product of Splitted Binary Tree
·2 mins
LeetCode Medium Tree Binary-Tree Dfs
LeetCode 1339 note: compute the total, enumerate every subtree sum, and leave the input untouched
LeetCode 1306: Jump Game III
·2 mins
LeetCode Medium Bfs Dfs Graph Array
LeetCode 1306 note: indices are nodes and the two jumps are edges; BFS or DFS decides whether a zero is reachable