-
The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the below in invoked as height (root) to compute the height of a binary tree rooted at the tree pointer root.
{ if (n = = NULL) return – 1;
if (n → left = = NULL)
if (n → right = = NULL) return 0;
The appropriate expressions for the two boxes B1 and B2 are
-
- B1 : (1 + height (n → right) B2 : (1 + max (h1, h2)
- B1 : (height (n → right) B2 : (1 + max (h1, h2)
- B1 : (height (n → right) B2 : max (h1, h2)
- B1 : (1 + height (n → right) B2 : max (h1, h2)
- B1 : (1 + height (n → right) B2 : (1 + max (h1, h2)
Correct Option: A
The box B1 gets executed when left subtree of n is NULL and right subtree is not NULL. In this case, height of n will be height of right subtree plus one.
The box B2 gets executed when both left and right subtrees of n are not NULL. In this case, height of n will be max of heights of left and right sbtrees of n plus 1.