Graph Theory37 sections · 1633 units
Open in Course

LeetCode 543 Diameter of Binary Tree - Lessons

summary

The DP approach fits naturally into tree recursion. You do not need special setup, track an extra variable.

The pattern: compute something for children, combine for current node, update global answer. This template solves many tree problems: diameter, longest path, maximum sum path.

Returning height while updating diameter shows how one traversal can serve multiple purposes. When you see a tree problem asking for a global property, consider whether post-order DFS with a global variable works.