GATE / CS ARCHIVE

SIMILAR QUESTIONS / DATA STRUCTURES

Derive recursive rooted-tree height

3 questions solved the same way, newest first. Back to Data Structures

How these are solved

Return the correct empty/singleton base case and one plus the larger child height; fill or interpret recursion under the stated height convention. 2001 counts nodes while others count edges;

2 marks · MCQ
Report issue

Recursive rooted height and wording mismatch

Tree Properties & Traversals

Question 47, GATE 2012: The source defines tree height as “the number of edges on the longest path in the tree” and gives this rooted-tree routine. Fill B1,B2:
int height(treeptr n) {
 if(n==NULL) return -1;
 if(n->left==NULL)
   if(n->right==NULL) return 0; else return B1;
 else { h1=height(n->left);
   if(n->right==NULL) return 1+h1;
   else { h2=height(n->right); return B2; }
 }
}
(A) B1=1+height(n->right), B2=1+max(h1,h2).
(B) B1=height(n->right), B2=1+max(h1,h2).
(C) B1=height(n->right), B2=max(h1,h2).
(D) B1=1+height(n->right), B2=max(h1,h2).
Tap to enlarge
Your answer
Answer & solution

Official answer: A

For the conventional rooted height, a leaf has height 0 and every parent-to-child step adds one edge. If only the right child exists, B1 must be 1+height(n→right). If both exist, B2 must be 1+max(h1, h2). Thus A is the intended rooted-height answer and matches the official key. However, the opening sentence literally defines the longest path anywhere in the tree, which is its diameter, not necessarily its root-to-leaf height. A root with two leaf children has rooted height 1 but diameter 2; none of the listed B2 expressions returns 2 for that example. Preserve this wording defect rather than claiming the code computes the literal definition.

Check: Check a node with only a right leaf: the child returns 0 and the parent must return 1, forcing B1’s +1. With two children, choose the larger subtree height and add the edge from the current node, forcing B2’s +1.

Original paper, page 12 · Answer key

2 marks · MCQ
Report issue

Recursive binary-tree height

Tree Properties & Traversals

Question 43, GATE 2004: Consider a binary-tree node with fields leftChild, element and rightChild, and this function (max returns the larger argument):
int DoSomething(struct CellNode *ptr) {
  int value=0;
  if(ptr!=NULL) {
    if(ptr->leftChild!=NULL)
      value=1+DoSomething(ptr->leftChild);
    if(ptr->rightChild!=NULL)
      value=max(value,1+DoSomething(ptr->rightChild));
  }
  return value;
}
What is returned when ptr is the root of a nonempty tree?
Options:
(A) Number of leaf nodes
(B) Number of nodes
(C) Number of internal nodes
(D) Height of tree
Question 43, GATE 2004: Consider a binary-tree node with fields leftChild, element and rightChild, and this function (max returns the larger argument):
int DoSomething(struct CellNode *ptr) {
  int value=0;
  if(ptr!=NULL) {
    if(ptr->leftChild!=NULL)
      value=1+DoSomething(ptr->leftChild);
    if(ptr->rightChild!=NULL)
      value=max(value,1+DoSomething(ptr->rightChild));
  }
  return value;
}
What is returned when ptr is the root of a nonempty tree?
Options:
(A) Number of leaf nodes
(B) Number of nodes
(C) Number of internal nodes
(D) Height of tree
Tap to enlarge
Your answer
Answer & solution

Answer: D (no official key was published)

1. A leaf returns zero. A node with a child takes one plus that child’s recursively computed value; with two children it takes the maximum.

2. Induction gives the maximum number of edges from the node to a descendant leaf, i.e. height measured in edges. The null check returns zero but no missing child adds an edge.

3. The code does not count nodes or leaves.

4. Teaching check: A singleton tree returns zero, immediately excluding both total-node and leaf counts. A two-edge chain returns two. The recursive maximum chooses the deepest child path, while adding one accounts for the edge from the current node to that child.

Original paper, page 6

Descriptive
Report issue

Complete recursive tree depth

Tree Properties & Traversals

Question 14(c), GATE 2001: depth(t) returns 0 for NULL, sets x=depth(t->left), then needs S1, S2 and S3 to return the tree depth.
Question 14(c), GATE 2001: depth(t) returns 0 for NULL, sets x=depth(t->left), then needs S1, S2 and S3 to return the tree depth.
Tap to enlarge
Answer & solution

Answer: S1: y=depth(t->right); S2: x+1; S3: y+1. (no official key was published)

The null-tree base case fixes depth as the number of nodes on a longest root-to-leaf path. Each nonempty tree contributes its root plus the larger child depth. Full compilable function:

#include <stddef.h>
typedef struct node {
    int key;
    struct node *left, *right;
} *Tree;
int depth(Tree t) {
    int x, y;
    if (t == NULL) return 0;
    x = depth(t->left);
    y = depth(t->right); /* S1 */
    if (x > y) return x + 1; /* S2 */
    else return y + 1; /* S3 */
}

A leaf returns 1. The preceding tree returns 4. Each node is visited once, giving O(n) time and O(h) recursive stack space; an edge-count definition would require a different base case and is not the convention of the supplied program.

Original paper, page 9