Larg bst-------- // in line 47 why we add height instead of taking max of left and right

Hello @guptarahul3100,

Line 47 doesn’t make any sence:
return max(height(root->left)+height(root->right)) + 1 ;

What is the use of max operator here?

I have used max for returning the max length of the tree from left and right

Hey @himanshu123,

But it accepts two parameters and you are passing a single.
I think it should be:
return max(height(root->left)**,**height(root->right)) + 1 ;

Now, coming back to your initial question:
Why are we taking max of heights instead of left and right itself.

Line 47 is the part of height() function whose purpose is to compute the height of the tree:

root->left and root->right are references to the nodes that are at left and right of the current root.

Example:
For the following tree,

________________1
_________2 _____________3
_____4 _______5 ____6 ________7
_________________________________8

If you are at 1, and you know the height of right and left subtrees i.e.
height of left subtree: 2 (for path 2, 4 or 2, 5)
height of right subtree: 3 (for path 3, 7, 8)

So, height of tree considering 1 as root:
=max(height of left subtree, the height of right subtree) +1(1 is for the current node 1)
= max(2,3) +1
= 4
path: 1, 3, 7, 8

Hope, it is clear now.

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.