Trace diameter on a tree: --- with attached to . The diameter is the path --- or --- (length ). At each node, compute and = two longest paths going down.
The candidate diameter through this node is . At node : paths down are to (length ), to (length ), and to - (length ). Two longest: and . Candidate: . The global maximum of all candidates gives the diameter. Post-order traversal keeps children are processed before parents.