Question 8576 – Data-Structures
April 17, 2024Question 8117 – Data-Structures
April 18, 2024Data-Structures
|
Question 20
|
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.

The appropriate expression 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) |
Question 20 Explanation:
Now, we analyze the code,
The box B1 gets executed when left sub tree of n is NULL & right subtree is not NULL.
In such case, height of n will be the height of the right subtree + 1.
The box B2 gets executed when both left & right subtrees of n are not NULL.
In such case, height of n will be max of heights of left & right subtrees of n+1.
Correct Answer: A
Question 20 Explanation:
Now, we analyze the code,
The box B1 gets executed when left sub tree of n is NULL & right subtree is not NULL.
In such case, height of n will be the height of the right subtree + 1.
The box B2 gets executed when both left & right subtrees of n are not NULL.
In such case, height of n will be max of heights of left & right subtrees of n+1.
