Reputation: 1
Can you make me understand this code to find the height of a binary tree:
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
/* use the larger one */
if (lDepth > rDepth)
return(lDepth+1);
else return(rDepth+1);
I am not able to understand how "int lDepth = maxDepth(node->left);" will return the height of left subtree as when it reaches the base case...it will return 0. (code is incomplete).
Upvotes: 0
Views: 143
Reputation: 2018
i am not able to understand how "int lDepth = maxDepth(node->left);" will return the height of left subtree as when it reaches the base case...it will return 0. (code is incomplete).
True it will return 0, but don't forget the +1, imagine it reaches a case where the tree has a left and right node (no grandchild), the height of the left is 0 and of the right is 0 so depth of the parent is the depth of largest +1 which is 1
Upvotes: 2