Depth-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 834: Sum of Distances in Tree
·3 mins
LeetCode
Hard
Dynamic-Programming
Tree
Depth-First-Search
Graph
Dp-on-Trees
LeetCode 834 note: rerooting DP: compute subtree sizes and ans[0] with root 0, then push ans[child] = ans[parent] - size + (n - size) to every node
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 968: Binary Tree Cameras
·3 mins
LeetCode
Hard
Dynamic-Programming
Tree
Depth-First-Search
Binary-Tree
Dp-on-Trees
LeetCode 968 note: post-order greedy with three states per node: has a camera, covered, not covered; place a camera only when a child is uncovered, and add one if the root is left uncovered
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 437: Path Sum III
·2 mins
LeetCode
Medium
Tree
Depth-First-Search
Binary-Tree
LeetCode 437 note: prefix sums on a tree with a hash map: look up prefix - target before adding the current prefix, and undo it when leaving the node
LeetCode 652: Find Duplicate Subtrees
·2 mins
LeetCode
Medium
Hash-Table
Tree
Depth-First-Search
Binary-Tree
LeetCode 652 note: encode every subtree in post-order and report it on its second occurrence; string keys are O(n^2) worst case, (val, left id, right id) ids make it O(n)
LeetCode 99: Recover Binary Search Tree
·2 mins
LeetCode
Medium
Tree
Depth-First-Search
Binary-Search-Tree
Binary-Tree
LeetCode 99 note: find the inversions in the in-order sequence: the earlier node of the first inversion and the later node of the last one were swapped
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 687: Longest Univalue Path
·2 mins
LeetCode
Medium
Tree
Depth-First-Search
Binary-Tree
Dp-on-Trees
LeetCode 687 note: DFS returns the longest same-value downward chain in edges, and each node updates the answer with left + right
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