Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

GATE | GATE-CS-2014-(Set-3) | Question 65

  • Difficulty Level : Medium
  • Last Updated : 08 Nov, 2017

Consider the pseudocode given below. The function DoSomething() takes as argument a pointer to the root of an arbitrary tree represented by the leftMostChild-rightSibling representation.
Each node of the tree is of type treeNode.

Attention reader! Don’t stop learning now.  Practice GATE exam well before the actual exam with the subject-wise and overall quizzes available in GATE Test Series Course.

Learn all GATE CS concepts with Free Live Classes on our youtube channel.




typedef struct treeNode* treeptr;
struct treeNode
{
    treeptr leftMostChild, rightSibling;
};
int DoSomething (treeptr tree)
{
    int value=0;
    if (tree != NULL)
    {
        if (tree->leftMostChild == NULL)
            value = 1;
        else
            value = DoSomething(tree->leftMostChild);
        value = value + DoSomething(tree->rightSibling);
    }
    return(value);
}

When the pointer to the root of a tree is passed as the argument to DoSomething, the value returned by the function corresponds to the
(A) number of internal nodes in the tree.

(B) height of the tree.
(C) number of nodes without a right sibling in the tree.



(D) number of leaf nodes in the tree.


Answer: (D)

Explanation: The function counts leaf nodes for a tree represented using leftMostChild-rightSibling representation.

Below is function with comments added to demonstrate how function works.




int DoSomething (treeptr tree)
{
    // If tree is empty, 0 is returned
    int value = 0;
  
    // IF tree is not empty
    if (tree != NULL)
    {
        // IF this is a leaf node, then values is initialized as 1
        if (tree->leftMostChild == NULL)
            value = 1;
  
        // Else value is initialized as the value returned by leftmost
        // child which in turn calls for the other children of this node
        // Using last call "value = value + DoSomething(tree->rightSibling);"
        else
            value = DoSomething(tree->leftMostChild);
  
        // Add value returned by right sibling
        value = value + DoSomething(tree->rightSibling);
    }
    return(value);
}


Quiz of this Question




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!