The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+18 votes
1.5k views

Consider the following C program segment where $CellNode$ represents a node in a binary tree:

struct CellNode {
    struct CellNode *leftChild;
    int element;
    struct CellNode *rightChild;
};

int Getvalue (struct CellNode *ptr) {
    int value = 0;
    if (ptr != NULL) {
        if ((ptr->leftChild == NULL) &&
            (ptr->rightChild == NULL))
            value = 1;
        else
            value = value + GetValue(ptr->leftChild)
                      + GetValue(ptr->rightChild);
    }
    return(value);
}

The value returned by $GetValue$ when a pointer to the root of a binary tree is passed as its argument is:

  1. the number of nodes in the tree

  2. the number of internal nodes in the tree

  3. the number of leaf nodes in the tree

  4. the height of the tree

asked in DS by Veteran (59.6k points)
edited by | 1.5k views

2 Answers

+20 votes
Best answer

Answer: C

As the function returns $1$ if and only if any node has both left & right children as $NULL$ (that node is a leaf node). Hence, value gets incremented at each leaf node.

answered by Boss (34.1k points)
edited by
+1
But are all nodes being visited?
+4

Oh, I didn't see the question here. The else statement is as follows in the question paper:

value = value + getValue(ptr->leftChild) + getValue(ptr->rightChild)
+2
It was a typo. Corrected now. But beware of such stuffs in GATE :)
+2
Please remove the picture attached with this question (as it still has the typo). Ya, I know what IIT-K did this year. :)
0
whats the typo in this question. It seems to be correct. What am i missing???
+2
Typo was corrected.
+1
Better to consider a level 2 tree then try to run this function ...
–9 votes
Assumption: the binary tree is left justified Option a,b,c are absolutely wrong..it measures the height of the tree
answered by Boss (14.4k points)
Answer:

Related questions



Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

42,557 questions
48,550 answers
155,299 comments
63,512 users