0

I am working on a code to calulate difference between sum of odd and even levels of a BST. Here I have used a statement s = s2;

Structure definition : struct stack{ TNode *ar[100]; int top; }; Function : int calc(TNode *root){ stack *s; s = (stack *) malloc(sizeof(stack)); s->top = -1; int oSum = 0; // to keep odd level elements' sum int eSum = 0; // to keep even level elements' sum oSum += root->data; push(s, root); int level = 1; while(!isEmpty(s)){ stack *s2; s2 = (stack*) malloc(sizeof(stack)); s2->top = -1; printf("Iteration: %d ", ++it); while(!isEmpty(s)){ TNode* temp = (TNode*) malloc(sizeof(TNode)); temp = pop(s); //printf("Pop : %d \n", temp->data); if(temp->left != NULL) push(s2, temp->left); if(temp->right != NULL) push(s2, temp->right); if(level = 0){ if(temp->left != NULL) oSum += temp->left->data; if(temp->right != NULL) oSum += temp->right->data; } else{ if(temp->left) eSum += temp->left->data; if(temp->right) eSum += temp->right->data; } } /****** HERE*******/ s = s2; level = toggleLevel(level); } printf("\n\noSum: %d ESum: %d", oSum, eSum); return oSum - eSum; }

1st Dec 2017, 3:41 AM
Ashutosh
Ashutosh - avatar
13 Respostas
+ 1
Sorry I drove you to feel like RudeMan. :D I thought the other way. Consider this as a misunderstanding and please accept my apologies. Now :D peekstack() was just to check the elements in the stack. We may remove that. That doesn't count in the logic to this problem. And let me know whether you have understood the logic to the solution of the problem?
1st Dec 2017, 10:39 AM
Ashutosh
Ashutosh - avatar
+ 1
shit😢😢😢😢😢😢😢 damm damm damm..!
1st Dec 2017, 11:45 AM
Ashutosh
Ashutosh - avatar
0
That's cool. But why that thing is not working?
1st Dec 2017, 7:16 AM
Ashutosh
Ashutosh - avatar
0
I am talking about my thing. Your thing might work but I am sticking with mine only. As you asked for the whole source: https://repl.it/@sharmaAshutosh/Difference-of-the-sum-of-oddeven-levels-of-the-BST Hope you will be "NOT RUDE" this time.
1st Dec 2017, 8:34 AM
Ashutosh
Ashutosh - avatar
0
Hope you will reply fast as I am stuck. Help is always appreciable, thanks for your time. :)
1st Dec 2017, 8:38 AM
Ashutosh
Ashutosh - avatar
0
Yeah, I have tried it without peekstack() and other debugging calls. But unfortunately, It is not giving the right answer. It is only working on top 2 levels. I have removed all debugging statements for making the code more readable and convenient for you.Check again for a neat code.Also, I added comments on line 119 our concern is to make line 124 work. P.S - If you have lost the link to the source: https://repl.it/@sharmaAshutosh/Difference-of-the-sum-of-oddeven-levels-of-the-BST
1st Dec 2017, 11:02 AM
Ashutosh
Ashutosh - avatar