Difference between the 2 approaches

what is the difference between the optimised approach of calculating diameter and the traditional way to do so, since we are calculating heights in both the cases.

Hello @gargprachi1203 using class we have encapsulated some parameters which means that we calculate the variables simulataneously instead of making calls at every node to calculate the whole height again and again. in the brute force approach for calculating the height for every node again and again so for n nodes we have to calcualte the height n times and the time complexity will be O(n^2).
but using optimized approach we will begin from the leaf node and store heights while traversing the tree.For every node we will return height of that node and diameter of that node. These stored heights and diameter will help in computing the height and diameter of the parent node. Since this is just a postorder traversal, so complexity is 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.