Binary-Search-Tree
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 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 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 1373: Maximum Sum BST in Binary Tree
·2 mins
LeetCode
Hard
Dynamic-Programming
Tree-Dp
Binary-Search-Tree
Depth-First-Search
Tree
LeetCode 1373 note: a post-order traversal returns (sum, min, max, is_bst); a node roots a BST when the left max < its value < the right min
LeetCode 109: Convert Sorted List to Binary Search Tree
·2 mins
LeetCode
Medium
Linked-List
Divide-and-Conquer
Tree
Binary-Search-Tree
LeetCode 109 note: take the middle found with fast and slow pointers as the root and recurse on both halves; the height balances naturally
LeetCode 98: Validate Binary Search Tree
·3 mins
LeetCode
Medium
Tree
Depth-First-Search
Binary-Search-Tree
Binary-Tree
LeetCode 98 note: every node has a valid range (low, high); going left the upper bound becomes the node and going right the lower bound does; comparing parent and child is not enough
LeetCode 230: Kth Smallest Element in a BST
·1 min
LeetCode
Medium
Tree
Depth-First-Search
Binary-Search-Tree
Binary-Tree
LeetCode 230 note: a BST’s in-order traversal is ascending, so stop an iterative in-order walk at the k-th node
LeetCode 938: Range Sum of BST
·2 mins
LeetCode
Easy
Tree
Depth-First-Search
Binary-Search-Tree
Binary-Tree
LeetCode 938 note: prune with the BST order, going only right below low and only left above high, adding nodes inside the range
LeetCode 897: Increasing Order Search Tree
·1 min
LeetCode
Easy
Stack
Tree
Depth-First-Search
Binary-Search-Tree
LeetCode 897 note: in-order traversal appending nodes to a right-only chain with left children cleared; keep the dummy separate from the moving tail
LeetCode 783: Minimum Distance Between BST Nodes
·2 mins
LeetCode
Easy
Tree
Depth-First-Search
Breadth-First-Search
Binary-Search-Tree
LeetCode 783 note: a BST’s in-order is ascending, so the minimum difference is between in-order neighbors; remember the previous value and compare