Graph Theory37 sections · 1633 units
Open in Course

LeetCode 543 Diameter of Binary Tree - Implementation Details

Recursive height with diameter update

The implementation follows the Tree DP template:

1.1. Define a recursive function that returns height.

2.2. At each node, compute left height and right height.

3.3. Update diameter = max(diameter, leftH + rightH) as a side effect.

4.4. Return 1 + max(leftH, rightH) to the parent.

This runs in O(n)O(n) time and O(h)O(h) space. You will write the full code in the Tree Diameter & Center section.