GATE CS 2012 · Arrays & Matrix Representation

MCQ · 2 marks · Problem PROG-DSTR-0160

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    \textbf{B1};
   else { h1 = height (n -> left);
   if (n -> right == NULL) return (1+h1);
   else { h2 = height (n -> right);
   return   \textbf{B2};
    }
  }
}
The appropriate expressions for the two boxes B1 and B2 are
  1. A
    B1: (1+height(n \(\rightarrow\) right)) B2: (1+max(h1, h2))
  2. B
    B1: (height(n \(\rightarrow\) right)) B2: (1+max(h1,h2))
  3. C
    B1: height(n \(\rightarrow\) right) B2: max(h1, h2)
  4. D
    B1: (1+ height(n \(\rightarrow\) right)) B2: max(h1, h2)

Solve it on GATE DA practice →More Basic Data Structures problems

Show the worked solutionOfficial GATE Answer Key: Option A.See full step-by-step discussion on GateOverflow: https://gateoverflow.in/2163/gate2012-47

GATE DA practice: past exam problems by chapter, with worked solutions · gatedaprep.com