Binary Search Trees

BSTs combine binary tree structure with ordering. Left is smaller, right is larger for O(log n) search.

35 lessons
188 min

Lessons

1. Intro

Why BSTs matter

2m

2. BST Property

The ordering invariant

3m

3. Inorder Gives Sorted Order

The key observation

3m

4. BST Search

Binary search in tree form

3m

5. BST Insertion

Finding the right find

3m

6. BST Deletion

Three cases to handle

4m

7. Problem - Validate BST

Is this tree a valid BST?

3m1 problems

8. Validate BST: Range Approach

Pass valid range down

4m

9. Validate BST Solution

Implement your solution

12m1 problems

10. Problem - Search in BST

Find a node by value

2m1 problems

11. Search in BST Solution

Apply the BST property

8m1 problems

12. Problem - Insert into BST

Add a new value

2m1 problems

13. Insert into BST Solution

Find the leaf position

10m1 problems

14. Problem - Delete Node in BST

Maintain BST property

3m1 problems

15. Delete Node: Finding Successor

The two-children case

3m

16. Delete Node in BST Solution

Handle all three cases

15m1 problems

17. Problem - Kth Smallest Element

Use inorder property

3m1 problems

18. Kth Smallest Solution

Stop at kth node

12m1 problems

19. Problem - LCA of BST

Easier than general trees

2m1 problems

20. LCA of BST Solution

Use the ordering property

10m1 problems

21. Why Balance Matters

The height problem

3m

22. AVL Tree Concept

Strictly balanced BST

4m

23. When to Use BSTs

Practical considerations

3m

24. Problem - Sorted Array to BST

Build a balanced BST

3m1 problems

25. Sorted Array to BST Solution

Pick middle as root

10m1 problems

26. Problem - BST Iterator

Lazy inorder traversal

3m1 problems

27. BST Iterator Solution

Controlled stack traversal

15m1 problems

28. Problem - Recover BST

Fix swapped nodes

3m1 problems

29. Recover BST: Finding Violations

Inorder should be sorted

5m

30. Recover BST Solution

Implement your solution

15m1 problems

31. Floor and Ceiling

Find closest values

3m

32. BST from Preorder

Reconstruct from traversal

3m

33. Successor and Predecessor

Next/previous in sorted order

3m

34. Quiz: Binary Search Trees

Test your understanding

5m1 problems

35. Section Recap

What you learned

3m

Practice Problems

1.
MultisetCodeforcesmedium

K-th order statistics with deletions. Teaches Fenwick tree or segment tree as BST alternative due to memory constraints.

2.
Glass CarvingCodeforcesmedium

Track maximum segment length with set/multiset. Dynamic insertion and deletion with ordered queries.

3.
Two TVsCodeforcesmedium

Interval scheduling using ordered set. Efficiently track end times for greedy assignment.

4.

BFS with ordered set for lexicographically smallest path. Priority-based exploration using BST properties.

5.
Two HeapsCodeforcesmedium

Distribute elements into two heaps maximizing distinct pairs. Frequency-based ordering decisions.

6.
Treasure of The SegmentsCodeforcesmedium

Count non-intersecting segments using binary search on sorted set. Efficient range queries.

7.
Roads and LibrariesCodeforcesmedium

Range query optimization with BST-like structure for efficient lookups and updates.

8.
Sereja and BracketsCodeforcesmedium

Segment tree for bracket sequences. BST-like range query structure for bracket matching.

9.
Array QueriesCodeforceshard

Combine sqrt decomposition with precomputation. BST-like thinking for query optimization.

10.
Palindrome PairsCodeforcesmedium

Count palindromic substrings with ordered structure. DP with BST-style range considerations.

11.
Nearest Opposite ParityCodeforcesmedium

Multi-source BFS with ordered processing. Distance computation using sorted structure.

12.

Petya and Array: count subarrays with sum < t. BIT/segment tree for order statistics.

13.

Check BST property with range bounds. Teaches passing min/max constraints through recursion.

14.

Inorder traversal gives sorted order. Foundation for order statistics in BST.

15.

Find next greater node. Teaches BST property exploitation for O(h) successor finding.

16.

Build height-balanced BST from sorted array. Divide and conquer with middle element as root.

17.
Delete Node in a BSTLeetCodemedium

Handle all deletion cases: leaf, one child, two children. Uses inorder successor replacement.

18.
BST IteratorLeetCodemedium

Controlled inorder traversal with O(h) space. Teaches iterative traversal with explicit stack.

19.
Range Sum of BSTLeetCodeeasy

Sum nodes in range [low, high]. Prune branches using BST property for efficiency.

20.

Find two swapped nodes using inorder traversal. Detect inversions in sorted sequence.

Ready to start learning?

Access all 35 lessons with interactive content and progress tracking.