1.6k views

Consider the following C program segment

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

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);
}

The value returned by the function $\text{DoSomething}$ when a pointer to the root of a non-empty tree is passed as argument is

1. The number of leaf nodes in the tree
2. The number of nodes in the tree
3. The number of internal nodes in the tree
4. The height of the tree
in DS | 1.6k views
+12

1st if calculates the height of the left subtree and 2nd if computes the height of the right subtree and takes the max of (height of left subtree, height of right subtree).

0
as program is comparing values from left and right sub tree , hence result can not be count of leaf , internal nodes , total nodes hence option a,b,c can be  eliminated

(D) It calculates Height of tree.

Easy way to get this answer .

Draw a tree where all $4$ parameters are different.

Get a Tree for which Height, No of Internal Nodes & No of Leafs are different & Trace out this algorithm.

by Boss (41.2k points)
edited by
+1

The below diagram may make the code easier to understand. It actually calculates the height of the tree

How I did that: I drew a tree then just tried the algo on this tree and then I modified the tree wisely,  then I tried the algo one more time
by Boss (14.3k points)
+1
Wow ... Thug life (y) ...
+1

How I did that: I drew a tree then just tried the algo on this tree and then I modified the tree wisely,  then I tried the algo one more time

what kind of explanation is this? :D :D

1