Algorithm general templates
Tree traverse
void traverse(TreeNode root) {
if (root == null) return;
traverse(root.left);
traverse(root.right);
}Graph traverse
void traverse(Node root) {
if (root == null) return;
for (Node child : root.children) {
traverse(child);
}
}Graph DFS traverse
boolean[] visited = new boolean[len];
void traverse(Graph graph, int v) {
if (visited[v]) return;
visited[v] = true;
for (Node neighbor : graph.neighbors(v)) {
traverse(graph, neighbor);
}
}
void traverse2(Graph graph, int v) {
visited[v] = true;
for (int neighbor : graph.neighors(v)) {
if (!visited[neighbor]) {
traverse(neighbor);
}
}
}Binary Search
Sliding Window (Two pointers)
Tree
Inorder traversal a Binary Search Tree with iteration which will get a sorted array.
Preorder traverse binary tree using iteration way
Postorder traverse binary tree using iteration way
Find all ancestors for a node
BST
Back Tracking
Combination
Permutation
Graph
BFS
Leetcode 286
DFS
Leetcode 733
Dijkstra
Leetcode 743
Sort algorithms
Toplogical Sort
Union Find
Trie
Word Dictionary with . wildchards matching
Segment Tree
Typical Tree Recursion subproblem (use postorder)
Last updated