The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudocode below is invoked as height(root) to compute the height of a binary tree rooted at the tree pointer root. int height (treeptr n) { if (n == NULL) return -1;   if (n →  left == NULL)        if (n →  right == NULL) return 0;        else return ; \(\boxed{B1}\);     // Box 1 else { h1 = height (n →  left);          if (n → right == NULL) return (1+h1);          else { h2 = height (n → right);          return ; \(\boxed{B2}\)   // Box 2                        }            } } The appropriate expressions for the two boxes B1 and B2 are

The height of a tree is defined as the number of edges on the longest path in the tree. The function shown in the pseudocode below is invoked as height(root) to compute the height of a binary tree rooted at the tree pointer root. int height (treeptr n) { if (n == NULL) return -1;   if (n →  left == NULL)        if (n →  right == NULL) return 0;        else return ; \(\boxed{B1}\);     // Box 1 else { h1 = height (n →  left);          if (n → right == NULL) return (1+h1);          else { h2 = height (n → right);          return ; \(\boxed{B2}\)   // Box 2                        }            } } The appropriate expressions for the two boxes B1 and B2 are Correct Answer <p>B1: (1+height(n → right))</p> <p>B2: (1+max(h1, h2))</p>

The correct answer is option 1

EXPLANATION:

The box B1 gets executed when left subtree of n is NULL and right subtree is not NULL. So, 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. So, height of n will be max of heights of left and right subtrees of n plus 1.

So, the correct answer is 

B1: (1+height(n → right))

B2: (1+max(h1, h2))

EXAMPLE:

Find the height of this tree using option 1 in the program

int height (treeptr n)

{ if (n == NULL) return -1;

  if (n →  left == NULL)

       if (n →  right == NULL) return 0;

       else return (1+height(n → right)) ;     // Box 1

else { h1 = height (n →  left);

         if (n → right == NULL) return (1+h1);

         else { h2 = height (n → right);

         return (1+max(h1, h2)) ; // Box 2

                       }

           }

}

[ alt="F1 Shubham.B 09-07-21 Savita D1" src="//storage.googleapis.com/tb-img/production/21/07/F1_Shubham.B_09-07-21_Savita_D1.png" style="width: 597px; height: 271px;">

So, the height of the above tree is 3

Related Questions

Each of the three kids gets at least one color box out of 6 color boxes, at least one tiffin box out of 6 tiffin box and at least one chocolate box out of 6 chocolate boxes so that the total number of the items that each of them gets is the same. No one gets the same number of tiffin boxes, color boxes and chocolate boxes. Then which of the following can be TRUE?