The height of a tree is defined as the number of edges on the longest path in…
GATE · 2012 · CS · Computer Science & IT
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 B1; // Box 1
else { h1 = height (n → left);
if (n → right == NULL) return (1+h1);
else { h2 = height (n → right);
return B2; // Box 2
}
}
}The appropriate expressions for the two boxes B1 and B2 are
- A.
B1: (1+height(n
\(\to\)right))B2: (1+max(h1, h2))
- B.
B1: (height(n
\(\to\)right))B2: (1+max(h1,h2))
- C.
B1: height(n
\(\to\)right)B2: max(h1, h2)
- D.
B1: (1+ height(n
\(\to\)right))B2: max(h1, h2)
Attempted by 240 students.
Show answer
Correct answer: A
The worked solution is available to enrolled students.
Video solution available to enrolled students.
Explore the full course: Iocl Engineers Officers Grade A Paper 2
Loading lesson…