Breadth-First-Search
LeetCode 2385: Amount of Time for Binary Tree to Be Infected
·2 mins
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 2385 note: turn the tree into an undirected graph and BFS from start; the number of levels minus one is the answer
LeetCode 2858: Minimum Edge Reversals So Every Node Is Reachable
·2 mins
LeetCode
Hard
Dynamic-Programming
Depth-First-Search
Breadth-First-Search
Graph
LeetCode 2858 note: rerooting DP: count the reversals needed with root 0, then moving the root to a child adds 1 if that edge pointed down and subtracts 1 if it pointed up
LeetCode 863: All Nodes Distance K in Binary Tree
·2 mins
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 863 note: turn the tree into an undirected graph (adding edges to parents), then BFS k steps from target
LeetCode 449: Serialize and Deserialize BST
·2 mins
LeetCode
Medium
String
Tree
Depth-First-Search
Breadth-First-Search
Design
Binary-Search-Tree
Binary-Tree
LeetCode 449 note: for a BST, pre-order without null markers is enough; deserialize recursively with bounds in O(n)
LeetCode 297: Serialize and Deserialize Binary Tree
·2 mins
LeetCode
Hard
String
Tree
Depth-First-Search
Breadth-First-Search
Design
Binary-Tree
LeetCode 297 note: pre-order with null markers identifies the tree uniquely; deserialize by consuming tokens from an iterator
LeetCode 515: Find Largest Value in Each Tree Row
·1 min
LeetCode
Medium
Tree
Depth-First-Search
Breadth-First-Search
Binary-Tree
LeetCode 515 note: BFS level order, taking the maximum of each level; start from -inf
LeetCode 429: N-ary Tree Level Order Traversal
·1 min
LeetCode
Medium
Tree
Breadth-First-Search
LeetCode 429 note: BFS level order with the children list in place of left and right
LeetCode 3787: Find Diameter Endpoints of a Tree
·2 mins
LeetCode
Medium
Tree
Breadth-First-Search
Graph
LeetCode 3787 note: two BFS passes find one diameter’s ends A and B; every node’s farthest node is A or B, so a node is a diameter endpoint exactly when max(dist(A,i), dist(B,i)) equals the diameter
LeetCode 314: Binary Tree Vertical Order Traversal
·2 mins
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Breadth-First-Search
Sorting
Binary-Tree
LeetCode 314 note: BFS with a column index; BFS is what guarantees top-to-bottom within a column and left-to-right within a row
LeetCode 773: Sliding Puzzle
·2 mins
LeetCode
Hard
Breadth-First-Search
Dynamic-Programming
Memoization
Matrix
LeetCode 773 note: there are only 6! = 720 boards, so flatten each board into a string as a BFS state, with swaps of 0 and a neighbor as the moves
LeetCode 4041: Minimum Operations to Form Subset Sum II
·2 mins
LeetCode
Hard
Dynamic-Programming
Knapsack
Breadth-First-Search
Array
LeetCode 4041 note: with operations in any order, BFS finds every value a number can become (up to sum) with the fewest steps, then the same grouped 0/1 knapsack as 4040
LeetCode 488: Zuma Game
·3 mins
LeetCode
Hard
Dynamic-Programming
Memoization
Breadth-First-Search
String
Stack
LeetCode 488 note: with at most 16 balls on the board and 5 in hand, memoize DFS on the whole (board, hand) state, inserting only before a same-colored ball or between two equal balls