↓Skip to main content

Medium

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 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 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