GATE CSE
First time here? Checkout the FAQ!
x
+3 votes
115 views

Consider the following code

asked in Programming by Veteran (65.1k points) 35 222 626 | 115 views
x is uninitialised..How to go about it..:)
x is no where used in the code. And the question asks for the sum of the internal nodes after the execution of that function. So i think ans is 22.
yes, x is not required in calculation . na?

@Habib

1 Answer

+5 votes
Best answer
It will change the value of all the nodes which are odd.

Means root->left  will become 0 from 1

and root->right will become 6 from 7

4  

0 6

6 4 2

8 2 8

Now sum of internal nodes wil be : 4 + 0 + 6 + 6 + 4 + 2 =  22
answered by Loyal (2.9k points) 3 17
selected by
//check the code for the above , run it on ide , 
//and tested it yourself with different test cases.
#include <bits/stdc++.h>
using namespace std;
struct treenode{
 int data;
 treenode *left;
 treenode *right;
};
treenode* create(int data){
   treenode *temp = (treenode*)malloc(sizeof(treenode));
   temp->data = data;
   temp->left = temp->right  = NULL;
   return temp;
}
int solve(treenode *root) { // will return 0 always
  int x;
  if(root == NULL) return 0;
  else
  if(root->data % 2 == 1){ // changing odd data items
      root->data -= 1;
      x++;
  }
  return solve(root->left) + solve(root->right);
}
void inorder(treenode *root) {
  if(root == NULL) return;
    inorder(root->left);
    cout << root->data <<" ";
    inorder(root->right);
}
int main(){
     treenode *root;
     root = create(4);
     root->left = create(1);
     root->right = create(7);
     root->left->left = create(6);
     root->left->right = create(4);
     root->right->left = create(2);
     root->left->left->right = create(8);
     root->left->right->left = create(2);
     root->right->left->left = create(8);
     cout << solve(root) << endl;
     inorder(root);
     cout << endl;
    return 0;
}

 



// output
0
6 8 0 2 4 4 8 2 6 

 


 

Related questions

0 votes
1 answer
1
asked in Programming by rahul sharma 5 Veteran (15k points) 12 109 316 | 85 views
+1 vote
1 answer
2
0 votes
1 answer
3
asked ago in Programming by stblue Boss (6.4k points) 3 11 32 | 68 views


Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
Top Users Oct 2017
  1. Arjun

    23398 Points

  2. Bikram

    17078 Points

  3. Habibkhan

    8280 Points

  4. srestha

    6300 Points

  5. Debashish Deka

    5438 Points

  6. jothee

    4978 Points

  7. Sachin Mittal 1

    4772 Points

  8. joshi_nitish

    4352 Points

  9. sushmita

    3970 Points

  10. Rishi yadav

    3804 Points


Recent Badges

Popular Question sh!va
Popular Question sh!va
Regular Rishabh Gupta 2
Popular Question Sunil8860
Reader Rajesh Veeranki 2
Notable Question rahul sharma 5
Commentator Shivam Chauhan
Notable Question set2018
Nice Comment srestha
Notable Question set2018
27,325 questions
35,177 answers
84,124 comments
33,280 users