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; }