Graph Theory37 sections · 1633 units
Open in Course

CSES 1131 Tree Diameter - Two Approaches Compared

DP vs Double BFS

You now know 22 ways to find tree diameter:

1.1. DP approach (LC 543543): Recurse on the tree, compute heights, track the maximum left + right sum. Works naturally on binary trees with pointer-based nodes.

2.2. Double BFS (CSES 11311131): Run BFS twice from endpoints. Works on any tree given as an adjacency list. Simpler when you do not need subtree information.

Both run in O(n)O(n) time. The Tree Diameter & Center section covers both in full detail with implementations and walkthroughs.