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

edited | 1.5k views

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.

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 ...
Assumption: the binary tree is left justified Option a,b,c are absolutely wrong..it measures the height of the tree

1
2