Tags

Binary Search Tree (BST)

Binary Search Tree (BST)

5 lessons
6 problems
1 question bank
7 community items

bst

Data Structures

4 lessons

Balanced BST (AVL / Red-Black)

Advanced

65 min

1 prereq

Insert the keys 1, 2, 3, 4, 5 into a plain BST in that order and you do not get a tree at all; you get a right-leaning chain with `O(n)` search. The fix is to detect imbalance after every insert or delete and rotate the offending subtree back into shape, which is exactly what a **Balanced BST** does. AVL and Red-Black trees are the two most influential designs in this family. This lesson covers the balance factor (height of left subtree minus height of right subtree), AVL's strict `|bf| <= 1` invariant enforced by the four rotation cases (LL, RR, LR, RL), and Red-Black trees' five color and structural invariants enforced by recoloring plus rotations. You will trace insertions through both schemes, see why Red-Black trees do fewer rotations on average and so are preferred by C++ `std::map` and Java `TreeMap`, and analyze the height bound (`<= 2 log n`) that gives both their worst-case `O(log n)` guarantee. In **Binary Search Tree (BST)**, you implemented search, insert, and delete that respected the ordering invariant but said nothing about shape; the resulting `O(n)` worst case is precisely what self-balancing solves by maintaining a height invariant in addition to the ordering invariant. Next, **B-Trees and B+ Trees** generalize the same balanced-tree idea to nodes that hold many keys at once, an essential design choice for disk-resident data.

Not Started

0%

Balanced BST
AVL Tree
Red-Black Tree
Binary Search Tree (BST)
Trees
Data Structures
Advanced
Premium
Comparison

Binary Search Tree (BST)

Intermediate

55 min

1 prereq

Run an inorder traversal on a **Binary Search Tree (BST)** of any size and the keys come out sorted, in `O(n)` time and `O(h)` space, with no separate sort step. That single property (an ordering invariant baked into the structure itself) is what turns a generic binary tree into a logarithmic-time search container that powers Java's `TreeMap`, C++ `std::map`, and the conceptual model behind every database index. This lesson covers the BST property (left subtree keys less than the node, right subtree keys greater), search and insert that follow the invariant, the three deletion cases (leaf, one child, two children resolved by inorder successor or predecessor), and the validation problem that interviewers love because the naive `node.left.val < node.val` check is wrong. You will also analyze the gap between the balanced `O(log n)` and the degenerate `O(n)` chain that motivates the next lesson. In **Trees: Binary Tree Fundamentals**, you implemented preorder, inorder, postorder, and level-order traversals; the BST property is what makes inorder special, because it now produces sorted output for free. Next, **Balanced BST (AVL / Red-Black)** addresses the elephant in the room: an unlucky insertion order can degrade a BST to a linked list, and self-balancing rotations are the fix.

Not Started

0%

Binary Search Tree (BST)
Binary Tree
Trees
Data Structures
Searching
Inorder
Intermediate
Premium
Recursion

Splay Tree

Advanced

65 min

1 prereq

Real workloads are rarely uniform. Web caches, compiler symbol tables, and network routers all access the same handful of items thousands of times in a row before moving on. A **Splay Tree** turns that observation into a balancing strategy: every access (search, insert, or delete) drags the touched node to the root through a sequence of rotations, so frequently used keys stay shallow and effectively pay `O(1)` after the first hit. This lesson covers the three splay rotation cases (zig, zig-zig, and zig-zag) that move a target node toward the root in pairs of levels, the modified search, insert, and delete operations that always end with a splay, and the amortized analysis that yields `O(log n)` per operation across any sequence of `m` operations even though individual operations can be `O(n)` in the worst case. You will also meet the working set theorem, which is the formal version of the temporal-locality intuition. In **Binary Search Tree (BST)**, accesses left the tree shape unchanged; balanced variants enforced strict structural invariants. The splay tree refuses both options and instead lets the access pattern itself reshape the tree, with no balance factor or color bit stored anywhere. Next, **Treap (Randomized BST)** keeps the same single-bit-of-extra-state simplicity but uses random priorities (rather than recent access) to control balance.

Not Started

0%

Splay Tree
Binary Search Tree (BST)
Trees
Data Structures
Advanced
Premium
Amortized Analysis
Deep Dive

Treap (Randomized BST)

Advanced

65 min

1 prereq

Implementing a Red-Black tree from memory under interview pressure is a known-hard task; the rotation cases alone are several hundred lines of careful pointer work. A **Treap** reaches the same expected `O(log n)` height in maybe sixty lines, with no color bits and no balance factor, by giving each node a random priority and asking the tree to satisfy two simple invariants at once. This lesson covers the dual structure (BST order on keys, heap order on randomly assigned priorities), the proof sketch that random priorities yield expected `O(log n)` height because the tree is shaped like a uniform random BST, and the two operations that make treaps unusually flexible: `split(key)` cuts the treap into 'less than' and 'greater or equal' pieces, and `merge` glues two treaps together when one contains only smaller keys. From those two primitives, insert, delete, k-th element, and even range reversal fall out naturally. You will also meet the implicit treap, where positions replace keys and the result behaves like a balanced array supporting `O(log n)` insert-at-index and slicing. In **Binary Search Tree (BST)**, you saw that an unlucky insertion order produced a chain; randomized priorities solve that by making any input look balanced in expectation, the same way randomized quicksort tames adversarial pivots. With treaps in hand, the curriculum next explores structures that exploit very different properties (such as bounded integer universes) to push performance beyond what comparison-based trees can offer.

Not Started

0%

Treap (Randomized BST)
Binary Search Tree (BST)
Trees
Data Structures
Advanced
Premium
Randomized Algorithms
Probability Basics

Algorithms

1 lesson

Tree Algorithms

Intermediate

65 min

2 prereqs

An invert-binary-tree problem famously got Max Howell rejected from Google in 2015, and the joke landed because the canonical solution is three lines of recursion. Trees show up everywhere in interviews precisely because they reward a particular kind of thinking: the answer for a node is almost always a function of the answers for its left and right subtrees, computed recursively, combined at the parent. **Tree Algorithms** trains that decomposition habit across a wide range of problems. You will implement BST insert, search, delete, validation, and the kth-smallest in-order trick. You will compute lowest common ancestor in both a generic binary tree and a BST. You will measure height, diameter, and width, and walk through path-sum problems, root-to-leaf enumeration, and the deceptively tricky maximum-path-sum-between-any-two-nodes. Structural operations include invert, mirror check, flatten to linked list, and serialize and deserialize. The lesson finishes with reconstructing a tree from inorder plus preorder or inorder plus postorder. In **Trees: Binary Tree Fundamentals**, you learned how nodes, children, and the four traversal orders work. **Recursion Fundamentals** gave you the call-stack mental model that every tree algorithm here relies on; tree recursion is essentially recursion with two recursive calls per frame. Next, **Graph Algorithms (Core)** removes the tree assumption (no cycles, no shared nodes) and tackles the more general traversal problems that result.

Not Started

0%

Algorithms
Trees
Binary Tree
Binary Search Tree (BST)
Tree Traversal
Recursion
Lowest Common Ancestor (LCA)
Intermediate
Premium

Practice Problems

6 problems

Lowest Common Ancestor of a BST

Free
Not Started
Easy

Given a binary search tree and two nodes, find their lowest common ancestor by leveraging the BST property.

Binary Tree
Binary Search Tree (BST)
DFS
Recursion
Lowest Common Ancestor (LCA)
Beginner

902

17

Convert Sorted Array to Binary Search Tree

Free
Not Started
Easy

Given a sorted array, convert it into a height-balanced binary search tree using divide and conquer.

Binary Tree
Binary Search Tree (BST)
Balanced BST
Divide and Conquer
Recursion
Beginner

724

24

Binary Search Tree Iterator

Free
Not Started
Medium

Implement an iterator over a BST that returns elements in ascending order (in-order traversal).

Binary Tree
Binary Search Tree (BST)
Stack
Iterator Design
Inorder
Intermediate

701

20

Kth Smallest Element in a BST

Free
Not Started
Medium

Given the root of a BST and an integer k, return the kth smallest value (1-indexed) using in-order traversal.

Binary Tree
Binary Search Tree (BST)
DFS
Inorder
Stack
Intermediate

613

18

Minimum Absolute Difference in BST

Free
Not Started
Medium

Given a BST, find the minimum absolute difference between the values of any two different nodes using in-order traversal.

Binary Tree
Binary Search Tree (BST)
DFS
Inorder
Intermediate

702

20

Validate Binary Search Tree

Free
Not Started
Medium

Given the root of a binary tree, determine if it is a valid binary search tree using bounds checking or in-order traversal.

Binary Tree
Binary Search Tree (BST)
DFS
Recursion
Inorder
Intermediate

739

18

Community

7 items
Article

Red-Black vs AVL: The Trade-off Nobody Explains

Why every standard library shipped red-black even though AVL is theoretically prettier: rotation counts on delete, color flips that beat pointer surgery, and the workloads where the difference actually shows up.

red-black-tree
avl-tree
balanced-bst
bst
trees

652

18

May 7, 2026

by @ethandubois

Article

Trees and Binary Search Trees

What trees buy you over arrays and hash tables, why a textbook BST is almost never enough, and the balanced variants that actually run in production.

binary-tree
bst
trees
balanced-bst
data-structures

1.1k

22

4.3 (11)

Apr 16, 2026

by @tylerperry

Problem
Medium
$6.99

Delete Node in a BST

Delete a value from a BST and return any valid resulting BST, handling the three child-count cases cleanly.

trees
bst
recursion

556

16

4.4 (12)

Mar 25, 2026

by @jamalvargas

Problem
Medium
$6.99

Recover Binary Search Tree

Two BST nodes have been swapped by mistake. Restore the BST in-place by finding and swapping them back.

trees
bst
dfs
recursion

921

5

4.3 (15)

Mar 22, 2026

by @ananyanakamura

Problem
Easy
Free

Two Sum IV - Input is a BST

Decide whether two distinct BST nodes sum to a target, ideally in O(n) time and O(h) extra space.

trees
bst
two-pointers
dfs

418

9

Feb 28, 2026

by @owentoure

Problem
Easy
Free

Range Sum of BST

Sum every BST value within an inclusive range, pruning subtrees that fall outside.

trees
bst
dfs
recursion

506

16

Feb 16, 2026

by @ninarossi

Problem
Medium
Free

Insert into a Binary Search Tree

Insert a new value into a BST while preserving the ordering invariant; any valid resulting BST is accepted.

trees
bst
recursion

624

16

4.2 (11)

Jan 24, 2026

by @davidmorgan