↓Skip to main content

LeetCode

LeetCode 701: Insert into a Binary Search Tree
·2 mins
LeetCode Medium Tree Binary-Search-Tree Binary-Tree
LeetCode 701 note: walk down the BST to an empty spot and attach the new node; an empty tree must return the new node
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
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 173: Binary Search Tree Iterator
·2 mins
LeetCode Medium Stack Tree Design Binary-Search-Tree Binary-Tree Iterator
LeetCode 173 note: flattening to an array is simple but uses O(n) space; the follow-up keeps the left spine on a stack for amortized O(1) and O(h) space
LeetCode 1372: Longest ZigZag Path in a Binary Tree
·2 mins
LeetCode Medium Dynamic-Programming Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 1372 note: each node returns two values, the longest zigzag starting left and starting right; going left extends the left child’s start-right value
LeetCode 124: Binary Tree Maximum Path Sum
·2 mins
LeetCode Hard Dynamic-Programming Tree Depth-First-Search Binary-Tree Dp-on-Trees
LeetCode 124 note: DFS returns the best downward chain and updates the answer with left chain + node + right chain at every node; drop negative chains
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 1186: Maximum Subarray Sum with One Deletion
·2 mins
LeetCode Medium Array Dynamic-Programming
LeetCode 1186 note: Kadane with one more state, ending here with no deletion or with one deletion; update the answer at every step
LeetCode 775: Global and Local Inversions
·3 mins
LeetCode Medium Array Math
LeetCode 775 note: turn a counting problem into an existence check and solve it in O(n)
LeetCode 23: Merge k Sorted Lists
·4 mins
LeetCode Hard Linked-List Heap Merge-Sort Divide-and-Conquer
LeetCode 23 note: a heap that holds only k heads, and divide-and-conquer pairwise merging