At every node, the longest path passing through it equals the left subtree depth plus the right subtree depth. If you compute this at every node and track the maximum, you get the diameter.
Write a recursive depth function that returns the height of the current subtree. At each node, before returning, compute leftDepth + rightDepth and update a global maximum.
This way, a single DFS traversal computes both the depth and the diameter in one pass. The diameter is a side effect of the depth calculation.