recategorized by
3,675 views
2 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___?

2 Answers

Best answer
5 5 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 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)
Position:
Show:

Related questions

1 1 vote
2 2 answers
2.0k
2.0k views
CHïntän ÞäTël asked Dec 10, 2018
1,959 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 0 votes
0 0 answers
736
736 views
sunaina rawat asked Nov 7, 2017
736 views
Consider programint foo(struct node *tree){if(tree==0)return 0;int lh=ht(tree->left);int rh=ht(tree->right);int ld=foo(tree->left);int rd=foo(tree->right);return max(lh+r...
0 0 votes
1 1 answer
630
630 views
0 0 votes
1 1 answer
1.1k
1.1k views