Tree Diameter & Center

Finding tree diameter with two-BFS and DP methods, computing tree center and radius, and solving all-nodes distance queries with rerooting DP.

45 lessons
125 min

Lessons

1. Intro

The Goal

3m

2. What is Tree Diameter

Longest possible path

2m

3. Why Diameter Matters

Network analysis applications

2m

4. The Naive Approach

Try all pairs

2m

5. The Two-BFS Trick

Start anywhere, go far twice

3m

6. Why Two-BFS Works

Intuition behind the proof

3m

7. Two-BFS Implementation

Code structure

3m

8. Quiz: Two-BFS Method

Knowledge check

2m1 problems

9. CSES 1131 Tree Diameter - Problem Statement

CSES 1131

2m1 problems

10. CSES 1131 Tree Diameter - Core Idea

Apply two-BFS directly

2m

11. CSES 1131 Tree Diameter - Algorithm

Step by step

3m

12. CSES 1131 Tree Diameter - Implementation

Pseudocode and code

4m1 problems

13. CSES 1131 Tree Diameter - Walkthrough

Example trace

3m

14. CSES 1131 Tree Diameter - Lessons

What you learned

2m

15. The DP Approach

Alternative diameter method

3m

16. DP Method - How It Works

Depth through each node

3m

17. DP Method - Implementation

Recursive DFS

4m

18. Two-BFS vs DP

When to use each

3m

19. Quiz: Diameter Methods

Knowledge check

2m1 problems

20. LeetCode 543 Diameter of Binary Tree - Problem Statement

LeetCode 543

2m1 problems

21. LeetCode 543 Diameter of Binary Tree - Core Idea

Use DP method

2m

22. LeetCode 543 Diameter of Binary Tree - Algorithm

Recursive DFS

3m

23. LeetCode 543 Diameter of Binary Tree - Implementation

Python solution

3m1 problems

24. LeetCode 543 Diameter of Binary Tree - Lessons

summary

2m

25. Tree Center

The most central node

3m

26. Finding the Center

Peeling leaves iteratively

3m

27. Center via Diameter

Middle of longest path

3m

28. Tree Eccentricity and Radius

Measuring centrality

3m

29. Quiz: Tree Center

Knowledge check

2m1 problems

30. CSES 1132 Tree Distances I - Problem Statement

CSES 1132

2m1 problems

31. CSES 1132 Tree Distances I - Core Idea

Reuse diameter endpoints

3m

32. CSES 1132 Tree Distances I - Algorithm

Three BFS approach

3m

33. CSES 1132 Tree Distances I - Implementation

Pseudocode

4m1 problems

34. CSES 1132 Tree Distances I - Walkthrough

Example trace

3m

35. CSES 1132 Tree Distances I - Lessons

core idea

2m

36. CSES 1133 Tree Distances II - Problem Statement

CSES 1133

2m1 problems

37. CSES 1133 Tree Distances II - Core Idea

Rerooting DP technique

4m

38. CSES 1133 Tree Distances II - Algorithm

Two-pass DFS

4m

39. CSES 1133 Tree Distances II - Implementation

Python code

4m1 problems

40. CSES 1133 Tree Distances II - Walkthrough

Example trace

3m

41. CSES 1133 Tree Distances II - Lessons

Rerooting pattern

3m

42. Quiz: Sum of Distances

Knowledge check

2m1 problems

43. Comparing the Problems

Different distance queries

3m

44. Common Mistakes

What to avoid

3m

45. Section Recap

What we learned

3m

Practice Problems

2.
3.
Tree DiameterCSESmedium
4.
Kefa and ParkCodeforcesmedium
5.
Minimum Maximum DistanceCodeforcesmedium
6.
Three Paths on a TreeCodeforceshard

Ready to start learning?

Access all 45 lessons with interactive content and progress tracking.