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___? Data Structures made-easy-test-series data-structures binary-tree + – firki lama 3.7k views answer comment Share Follow Print See all 19 Comments 19 19 Comments reply Show 16 previous comments Tendua commented Jan 17, 2017 reply Follow flag Yes, it is a wrong question, I miss read it. sorry for that still my focus was on tracing the program. The question was not having an option as compilation error so I assumed it is true. Ok, let me ask you. What you will do if you see this in the gate. Will you not answer it? 0 0 replyShare dd commented Jan 17, 2017 reply Follow flag I hope gate people will not forget malloc() after typing a pointer declaration. It's like forgetting one right curly brace for a left curly brace. 0 0 replyShare Tendua commented Jan 17, 2017 reply Follow flag Mistake happens. Well hope so. 0 0 replyShare Please log in or register to add a comment.
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 dd answered Jan 17, 2017 • selected Jan 17, 2017 by firki lama dd comment Share Follow 0 reply Please log in or register to add a comment.
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) Tendua answered Jan 17, 2017 Tendua comment Share Follow See all 3 Comments 3 3 Comments reply Rahul Jain25 commented Jan 17, 2017 reply Follow flag I also thought the same. But for that they have to crater new node by using malloc and assign its adress, which I think made easy forgot to include. 0 0 replyShare Tendua commented Jan 17, 2017 reply Follow flag yes, I also think so . they were focused on tracing the program 0 0 replyShare dd commented Jan 17, 2017 reply Follow flag wrong QS ..are indeed wrong ! 0 0 replyShare Please log in or register to add a comment.