Binary Search Trees
BSTs combine binary tree structure with ordering. Left is smaller, right is larger for O(log n) search.
Lessons
1. Intro
Why BSTs matter
2. BST Property
The ordering invariant
3. Inorder Gives Sorted Order
The key observation
4. BST Search
Binary search in tree form
5. BST Insertion
Finding the right find
6. BST Deletion
Three cases to handle
7. Problem - Validate BST
Is this tree a valid BST?
8. Validate BST: Range Approach
Pass valid range down
9. Validate BST Solution
Implement your solution
10. Problem - Search in BST
Find a node by value
11. Search in BST Solution
Apply the BST property
12. Problem - Insert into BST
Add a new value
13. Insert into BST Solution
Find the leaf position
14. Problem - Delete Node in BST
Maintain BST property
15. Delete Node: Finding Successor
The two-children case
16. Delete Node in BST Solution
Handle all three cases
17. Problem - Kth Smallest Element
Use inorder property
18. Kth Smallest Solution
Stop at kth node
19. Problem - LCA of BST
Easier than general trees
20. LCA of BST Solution
Use the ordering property
21. Why Balance Matters
The height problem
22. AVL Tree Concept
Strictly balanced BST
23. When to Use BSTs
Practical considerations
24. Problem - Sorted Array to BST
Build a balanced BST
25. Sorted Array to BST Solution
Pick middle as root
26. Problem - BST Iterator
Lazy inorder traversal
27. BST Iterator Solution
Controlled stack traversal
28. Problem - Recover BST
Fix swapped nodes
29. Recover BST: Finding Violations
Inorder should be sorted
30. Recover BST Solution
Implement your solution
31. Floor and Ceiling
Find closest values
32. BST from Preorder
Reconstruct from traversal
33. Successor and Predecessor
Next/previous in sorted order
34. Quiz: Binary Search Trees
Test your understanding
35. Section Recap
What you learned
Practice Problems
K-th order statistics with deletions. Teaches Fenwick tree or segment tree as BST alternative due to memory constraints.
Track maximum segment length with set/multiset. Dynamic insertion and deletion with ordered queries.
Interval scheduling using ordered set. Efficiently track end times for greedy assignment.
BFS with ordered set for lexicographically smallest path. Priority-based exploration using BST properties.
Distribute elements into two heaps maximizing distinct pairs. Frequency-based ordering decisions.
Count non-intersecting segments using binary search on sorted set. Efficient range queries.
Range query optimization with BST-like structure for efficient lookups and updates.
Segment tree for bracket sequences. BST-like range query structure for bracket matching.
Combine sqrt decomposition with precomputation. BST-like thinking for query optimization.
Count palindromic substrings with ordered structure. DP with BST-style range considerations.
Multi-source BFS with ordered processing. Distance computation using sorted structure.
Petya and Array: count subarrays with sum < t. BIT/segment tree for order statistics.
Check BST property with range bounds. Teaches passing min/max constraints through recursion.
Inorder traversal gives sorted order. Foundation for order statistics in BST.
Find next greater node. Teaches BST property exploitation for O(h) successor finding.
Build height-balanced BST from sorted array. Divide and conquer with middle element as root.
Handle all deletion cases: leaf, one child, two children. Uses inorder successor replacement.
Controlled inorder traversal with O(h) space. Teaches iterative traversal with explicit stack.
Sum nodes in range [low, high]. Prune branches using BST property for efficiency.
Find two swapped nodes using inorder traversal. Detect inversions in sorted sequence.