# Data Structures | Tree Traversals | Question 1

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; ` `   ``else` `   ``{ ` `       ``/* 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

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.

My Personal Notes arrow_drop_up
Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.