Open In App
Related Articles

Data Structures | Tree Traversals | Question 1

Improve Article
Save Article
Like Article

Following function is supposed to calculate the maximum depth or height of a Binary tree — the number of nodes along the longest path from the root node down to the farthest leaf node.

int maxDepth(struct node* node)
   if (node==NULL)
       return 0;
       /* compute the depth of each subtree */
       int lDepth = maxDepth(node->left);
       int rDepth = maxDepth(node->right);
       /* use the larger one */
       if (lDepth > rDepth)
           return X;
       else return Y;

What should be the values of X and Y so that the function works correctly?
(A) X = lDepth, Y = rDepth
(B) X = lDepth + 1, Y = rDepth + 1
(C) X = lDepth – 1, Y = rDepth -1
(D) None of the above

Answer: (B)

Explanation: If a tree is not empty, height of tree is
MAX(Height of Left Subtree, Height of Right Subtree) + 1

See program to Find the Maximum Depth or Height of a Tree for more details.

Quiz of this Question

Last Updated : 28 Jun, 2021
Like Article
Save Article
Similar Reads
Related Tutorials