LeetCode
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 146: LRU Cache
·3 mins
LeetCode
Medium
Hash-Table
Linked-List
Design
Doubly-Linked-List
LeetCode 146 note: a hash map finds nodes and a doubly linked list keeps the usage order; both get and updating an existing key must move the node to the front
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 889: Construct Binary Tree from Preorder and Postorder Traversal
·2 mins
LeetCode
Medium
Array
Hash-Table
Divide-and-Conquer
Tree
Binary-Tree
LeetCode 889 note: keep the current path on a stack; when its top equals the next postorder value that subtree is complete and is popped; new nodes go left first
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 450: Delete Node in a BST
·2 mins
LeetCode
Medium
Tree
Binary-Search-Tree
Binary-Tree
LeetCode 450 note: once the node is found: with zero or one child, splice the child in; with two, copy the right subtree’s minimum and delete that node
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 331: Verify Preorder Serialization of a Binary Tree
·2 mins
LeetCode
Medium
String
Stack
Tree
Binary-Tree
LeetCode 331 note: count open slots: start with 1, every node fills one, a non-null node opens two more; never go negative and end at exactly 0
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