recategorized by
1,848 views
2 votes
2 votes

Consider the following program:

void find(struct Node *node)

{

struct Node *ptr,*q;

q = (struct Node *)malloc(sizeof(struct Node));

q->left = NULL;

q->right = NULL;

if(node == NULL) return;

find(nod->left);

find(nod->right);

ptr = node->left;

q->value = node->value;

node->left = q;

node->left->left = ptr;

}

If the root of the following tree is passed to the above function, by main function the sum of all the keys in the resultant tree produced by find() is given by___?

recategorized by

2 Answers

Best answer
4 votes
4 votes
// Assuimg q pointer initialization :

    void find(struct Node *node) {
        struct Node *ptr,*q;
        
        q = (struct Node *)malloc(sizeof(struct Node));
        q->left  = NULL;
        q->right = NULL;

        if(node ==  NULL) return;
        
        find(nod->left);
        find(nod->right);
        
        1. ptr = node->left;
        2. q->value = node->value;
        3. node->left = q;
        4. node->left->left = ptr;
    }

for any non-null node:

Output :

 before 

  2
 1 3

 after 

        2
       / \
      /   \
     /     \
    2       3
   /       /  
  1       3    
 1              
selected by
0 votes
0 votes
Easy folow the procedure, code is doing -

Copying value of the node to q -> data.

saving the left node pointer to the ptr.

step 1 tree will be like

                           2

              1                     3

  (null) 1 ( not given)      

 

step 2

                           2

              1                     3

  (null) 1 ( not given)           (null) 3 ( not given) { 3 is the left child of 3 of earlier node}

 

step 3

 

                             2

                 2 ( not given)                           3

            1                             (null) 3 ( not given) { 3 is the left child of 3 of earlier node}

  (null) 1 ( not given)

Related questions

1 votes
1 votes
2 answers
1
CHïntän ÞäTël asked Dec 10, 2018
977 views
four vertices {A,B,C,D} is given which has only vertex D as a leaf total number of binary tree are possible when every binary tree has four node!
0 votes
0 votes
1 answer
3
0 votes
0 votes
1 answer
4