↓快轉到主要內容

Dfs

Tree 樹的遍歷與常見技巧
·7 分鐘
演算法 Tree Binary-Tree Dfs Bfs Bst
DFS 回傳值、BFS 層序、BST 上下界、往上找 LCA、樹的直徑與剪葉子
BFS 與圖的遍歷
·3 分鐘
演算法 Bfs Dfs Graph Matrix Union-Find
連通塊、網格 flood fill、多源 BFS、狀態空間 BFS、最短環,以及遞迴 DFS 的深度問題
Backtracking 回溯
·3 分鐘
演算法 Backtracking Recursion Dfs Pruning
選擇、遞迴、撤銷的模板,子集合、排列、限制條件、網格搜尋與剪枝
LeetCode 827: Making A Large Island
·2 分鐘
LeetCode Hard Dfs Bfs Matrix Union-Find
LeetCode 827 解題筆記:先幫每座島編號並記大小,再對每個 0 看四周有哪些不同的島,大小相加再加 1
LeetCode 3203: Find Minimum Diameter After Merging Two Trees
·2 分鐘
LeetCode Hard Tree Bfs Dfs Graph Tree-Diameter
LeetCode 3203 解題筆記:連接兩棵樹的中心,答案是 max(d1, d2, ceil(d1/2) + ceil(d2/2) + 1)
LeetCode 1028: Recover a Tree From Preorder Traversal
·2 分鐘
LeetCode Hard Tree Binary-Tree Dfs Stack String
LeetCode 1028 解題筆記:用 stack 維護從根到目前節點的路徑,stack 長度就是深度
LeetCode 323: Number of Connected Components in an Undirected Graph
·2 分鐘
LeetCode Medium Graph Dfs Bfs Union-Find
LeetCode 323 解題筆記:DFS 或 Union-Find 數連通塊;Union-Find 沒有路徑壓縮會慢很多
LeetCode 1377: Frog Position After T Seconds
·2 分鐘
LeetCode Hard Tree Dfs Bfs Graph Probability
LeetCode 1377 解題筆記:無向邊、seen 要記節點、提早到達目標時要看還能不能走
LeetCode 980: Unique Paths III
·1 分鐘
LeetCode Hard Backtracking Matrix Dfs
LeetCode 980 解題筆記:網格回溯,原地標記走過的格子,到終點時檢查是否走過所有空格
LeetCode 865: Smallest Subtree with all the Deepest Nodes
·1 分鐘
LeetCode Medium Tree Binary-Tree Bfs Dfs Lca
LeetCode 865 解題筆記:BFS 找最後一層再一起往上爬,或一次 DFS 回傳 (深度, 節點)
LeetCode 1339: Maximum Product of Splitted Binary Tree
·1 分鐘
LeetCode Medium Tree Binary-Tree Dfs
LeetCode 1339 解題筆記:先算總和,再枚舉每棵子樹的和,不要改到輸入
LeetCode 1306: Jump Game III
·1 分鐘
LeetCode Medium Bfs Dfs Graph Array
LeetCode 1306 解題筆記:每個 index 是節點、往左右跳是邊,BFS 或 DFS 看能不能走到值為 0 的位置