in Programming in C edited by
2,891 views
0 votes
0 votes

Consider the following C program

struct treenode
{
    int data;
    struct treenode *left;
    struct treenode *right;
}
int gate2015(struct treenode *root)
{
    int gate;
    if(root==NULL) return 0;
    if(root->data % 2==1)
    {
        root->data -=1;
        gate++;
    }
    else
     root->data += 1;
return(root -> data + gate2015(root -> left) + gate2015(root->right));
}

If the above code is executed on the following tree, then what is the output ?

I m getting $47$ - match it please

in Programming in C edited by
by
2.9k views

4 Comments

@arjun sir ....what do u mean by eye problem??
0
0
Ur question photo is not clear
0
0
Arjun Sir tried to say that take the photos in proper light :)
0
0

2 Answers

0 votes
0 votes
 if(root->data % 2==1)
    {
        root->data -=1;   ==> Decrements the data, if data is Odd.
else
     root->data += 1;  ==> Increments data if it is Even.
return(root -> data + gate2015(root -> left) + gate2015(root->right)); ==> Returns the sum of all data

So, 4 changes to 5, 1 changes to zero, 7 changes to 6, 6 to 7 and so on.

The result now will be $5+0+6+7+5+3+9+3+9 = 47$. 

 

 

0 votes
0 votes
YEAH 47.

In this program : Add all the node->data but condition is if node's data contain even number then increase by one then add or if  if node's data contain odd number then decrease by one then add.

4->5 , 1->0 , 6->7 , 8->9 , 4->5 , 2->3 , 7->6 , 2->3 , 8->9

5+0+7+9+5+3+6+3+9= 47

1 comment

Considering, gate is initialized to zero, then gate value will be two and after recursion completes the given function returns 47.
0
0

Related questions