Diameter of tree Optimization method

I did not understand how this method is more optimized than the previous method where time complexity was O(n^2)?
How is this method more efficient??

@aryamaan1011 hey basically hm is approach me ek hi recursion call me dono cheezein that is height and diameter dono mnga rhe hai so hm har node ko traverse krrhe hai ek hi bar so complexity will be O(n).

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.