Advanced Trees

You know the fundamentals. Now explore HLD, centroid decomposition, treaps, and Euler tour techniques for advanced tree problems.

42 lessons
137 min

Lessons

1. Intro

Beyond basic trees

3m

2. Heavy-Light Decomposition

Linearizing tree paths

4m

3. Heavy vs Light Edges

The classification

3m

4. Quiz: Light Edge Bound

Why O(log n)?

2m1 problems

5. Building Heavy Chains

DFS construction

4m

6. HLD Construction - Code

The implementation

4m

7. Path Queries with HLD

Using segment trees

4m

8. HLD Path Query - Code

The implementation

4m

9. Problem - Path Queries

HLD in action

3m1 problems

10. Path Queries - Analysis

Why HLD?

3m

11. Path Queries - Implementation

Complete solution

4m1 problems

12. Lessons from Path Queries

summary

2m

13. Centroid Decomposition

Divide and conquer on trees

4m

14. Finding the Centroid

The algorithm

3m

15. Centroid Finding - Code

The implementation

3m

16. Quiz: Centroid Property

Understanding the guarantee

2m1 problems

17. Centroid Decomposition - Structure

The centroid tree

4m

18. Centroid Decomposition - Code

Building the structure

4m

19. Problem - Distance Queries

Classic centroid problem

3m1 problems

20. Distance Queries - Analysis

Using centroid ancestors

4m

21. Distance Queries - Implementation

Complete solution

4m1 problems

22. Lessons from Distance Queries

summary

2m

23. Treaps - Introduction

Randomized BST

4m

24. Treap Properties

BST + Heap

3m

25. Quiz: Treap Structure

Understanding the shape

2m1 problems

26. Treap Operations - Split

Dividing by key

4m

27. Treap Operations - Merge

Combining treaps

4m

28. Treap Insert and Delete

Using split and merge

3m

29. Implicit Treaps

Position-based keys

4m

30. Implicit Treap - Split by Size

Position-based splitting

4m

31. Problem - Cut and Paste

Implicit treap in action

3m1 problems

32. Cut and Paste - Analysis

The split-merge dance

3m

33. Cut and Paste - Implementation

Complete solution

4m1 problems

34. Lessons from Cut and Paste

summary

2m

35. Euler Tour Technique

Flattening subtrees

4m

36. Euler Tour - Construction

The implementation

3m

37. Subtree Queries with Euler Tour

Range operations

3m

38. HLD vs Euler Tour vs Centroid

Choosing the right tool

3m

39. Problem - Subtree Queries

Euler tour application

3m1 problems

40. Subtree Queries - Implementation

Complete solution

3m1 problems

41. Challenge: Combined Techniques

Putting it together

3m

42. Section Recap

What you've learned

2m

Practice Problems

1.

Excellent introduction to implicit treap with split/merge and range reverse operations. Teaches implicit key concepts where positions aren't stored explicitly.

2.
Anton and PermutationCodeforceshard

Perfect for learning order statistics trees with dynamic updates. Requires counting inversions after swaps using treaps or policy-based data structures.

3.

Dynamic segment trees with coordinate compression for large ranges (n ≤ 10^9). Great for learning split and merge on intervals.

4.

Advanced 2D range query solvable with nested treaps. Teaches transforming permutation problems into geometric queries.

5.
Radio StationsCodeforceshard

Practice for order statistics trees (policy-based data structures). Requires coordinate compression and range counting.

6.
Messenger SimulatorCodeforceshard

Dynamic position tracking ideal for order statistics trees. Teaches efficient element movements and position queries in O(log n).

7.
Vasiliy's MultisetCodeforcesmedium

Binary trie for maximum XOR queries. Teaches tree-based approaches to bit manipulation problems as specialized tree structures.

8.
DestinyCodeforceshard

Classic persistent segment trees (Cartesian tree variant). Teaches handling range majority queries efficiently with small k.

9.
A Simple TaskCodeforceshard

Solvable with 26 segment trees or implicit treaps for range sorting. Excellent for understanding lazy propagation and range modifications.

10.
SequenceCodeforcesmedium

Teaches coordinate compression with balanced trees for DP optimization. Reduces O(n²) solutions to O(n log n) using trees.

11.

Part of SecondThread's dedicated Treap contest. Designed specifically to teach treap concepts with split/merge operations.

12.
Painting the Array IICodeforceshard

Greedy problem benefiting from balanced BST operations. Teaches maintaining dynamic segments with optimal decisions using ordered structures.

13.

Classic order statistics problem solvable with AVL/Red-Black trees. Perfect for learning self-balancing BST implementations.

14.
Count of Range SumLeetCodehard

Requires balanced BST with coordinate compression for range counting. Teaches transforming prefix sum problems into tree-based queries.

15.
Range ModuleLeetCodehard

Excellent for learning interval trees and TreeMap operations. Requires efficient interval merging and splitting.

16.
Falling SquaresLeetCodehard

Teaches segment trees with coordinate compression and lazy propagation. Great for handling large coordinate spaces efficiently.

17.

Perfect introduction to TreeSet/TreeMap (Red-Black trees). Teaches efficient interval management with O(log n) insertions and merges.

18.

Greedy problem requiring TreeSet for efficient 'next available' queries. Demonstrates practical use of balanced BST higher() operations.

19.

Advanced segment tree with candidate tracking. Teaches building trees for complex queries where nodes maintain aggregate information.

20.
My Calendar IIILeetCodehard

Solvable with dynamic segment trees or balanced TreeMap. Excellent for learning lazy propagation with sparse ranges.

Ready to start learning?

Access all 42 lessons with interactive content and progress tracking.