This site uses one functional cookie to keep feature rollouts consistent for you. Nothing is set until you choose. See the privacy notice.
Phase 2: Structures
Left is smaller, right is bigger: use it to skip half the tree.
Builds on Tree DFS, Binary Search.
The question to ask
Does the ordering let me skip one subtree, or would sorted order make this easy?
A binary search tree is binary search turned into a data structure. Everything in the left subtree is smaller than the node and everything in the right is bigger, so a search only goes one way, and an in-order traversal yields values in sorted order. Most BST questions are one of those two facts: walk down one side, or walk in order and treat it as a sorted array.
Time O(h) per operation, O(n) to traverse · Space O(h)
Write it from memory first, in an editor or on paper, then check. Recalling it is what makes it stick; reading it again mostly doesn't.
Kth Smallest: an iterative in-order walk with a stack, counting down k as nodes come off. Stops early instead of visiting the whole tree.
function kthSmallest(root: TreeNode | null, k: number): number {
const stack: TreeNode[] = [];
let node = root;
let remaining = k;
while (node || stack.length > 0) {
while (node) {
stack.push(node);
node = node.left;
}
const next = stack.pop() as TreeNode;
remaining--;
if (remaining === 0) return next.val;
node = next.right;
}
return -1;
}LCA in a BST: if both values are smaller, go left; both bigger, go right; otherwise this node splits them and is the answer. No recursion needed.
function lowestCommonAncestorBST(root: TreeNode | null, p: number, q: number): number {
let node = root;
while (node) {
if (p < node.val && q < node.val) node = node.left;
else if (p > node.val && q > node.val) node = node.right;
else return node.val;
}
return -1;
}Easy to hard. Read the prompt and name the signal before opening the tell. After each attempt, rate it: Again brings it back tomorrow, Hard holds the interval, Good and Easy push it further out.
Find the node in a binary search tree whose value equals val and return its subtree, or null.
"Binary search tree" search: go left if smaller, right if bigger. Never visit both.
Not attempted yet
Convert a sorted array to a height-balanced binary search tree.
"Sorted array to" a balanced BST: the middle element is the root, recurse on each half.
Not attempted yet
Return the minimum absolute difference between the values of any two different nodes in a BST.
"Minimum absolute difference" in a BST: closest values are neighbours in in-order, so compare each with the previous.
Not attempted yet
Given a binary search tree and two of its nodes, return their lowest common ancestor.
In a BST the LCA is the first node whose value falls between the two targets. Walk down; no recursion needed.
Not attempted yet
Determine whether a binary tree is a valid binary search tree, with strictly smaller values on the left and strictly bigger on the right, at every depth.
"Validate" a BST: pass down the allowed (low, high) range. Comparing only with children is the classic wrong answer.
Not attempted yet
Return the kth smallest value among all the nodes of a binary search tree.
"Kth smallest" in a BST: in-order is sorted, so stop at the kth node.
Not attempted yet
Insert a value into a binary search tree and return the root. The value is not already in the tree.
"Insert" into a BST: walk down as if searching, attach at the first empty spot.
Not attempted yet
Delete the node with a given key from a BST and return the root, keeping it a valid BST.
"Delete" in a BST has three cases: leaf, one child, two children. With two, copy the in-order successor up and delete it from the right subtree.
Not attempted yet
Implement an iterator over a BST that returns values in in-order with next and hasNext, using O(h) memory.
"Iterator" with O(h) memory: a stack holding the left spine; each next pops one and pushes the left spine of its right child.
Not attempted yet