# Difference between sums of odd position and even position nodes for each level of a Binary Tree

Given a Binary Tree, the task is to find the absolute difference between the sums of odd and even positioned nodes. A node is said to be odd and even positioned if its position in the current level is odd and even respectively. **Note** that the first element of each row is considered as odd positioned.

**Examples:**

Input:5 / \ 2 6 / \ \ 1 4 8 / / \ 3 7 9Output:11 Level oddPositionNodeSum evenPositionNodeSum 0 5 0 1 2 6 2 9 4 3 12 7 Difference = |(5 + 2 + 9 + 12) - (0 + 6 + 4 + 7)| = |28 - 17| = 11Input:5 / \ 2 3Output:4

**Approach:** To find the sum of nodes at even and odd positions level by level, use level order traversal. While traversing the tree level by level mark flag **oddPosition** as true for the first element of each row and switch it for each next element of the same row. And in case if **oddPosition** flag is true add the node data into **oddPositionNodeSum** else add node data to **evenPositionNodeSum**. After completion of tree traversal find the absolute value of their differences at the end of the tree traversal.

Below is the implementation of the above approach:

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `struct` `Node { ` ` ` `int` `data; ` ` ` `Node *left, *right; ` `}; ` ` ` `// Iterative method to perform level ` `// order traversal line by line ` `int` `nodeSumDiff(Node* root) ` `{ ` ` ` ` ` `// Base Case ` ` ` `if` `(root == NULL) ` ` ` `return` `0; ` ` ` ` ` `int` `evenPositionNodeSum = 0; ` ` ` `int` `oddPositionNodeSum = 0; ` ` ` ` ` `// Create an empty queue for level ` ` ` `// order traversal ` ` ` `queue<Node*> q; ` ` ` ` ` `// Enqueue root element ` ` ` `q.push(root); ` ` ` ` ` `while` `(1) { ` ` ` ` ` `// nodeCount (queue size) indicates ` ` ` `// number of nodes in the current level ` ` ` `int` `nodeCount = q.size(); ` ` ` `if` `(nodeCount == 0) ` ` ` `break` `; ` ` ` ` ` `// Mark 1st node as even positioned ` ` ` `bool` `oddPosition = ` `true` `; ` ` ` ` ` `// Dequeue all the nodes of current level ` ` ` `// and Enqueue all the nodes of next level ` ` ` `while` `(nodeCount > 0) { ` ` ` `Node* node = q.front(); ` ` ` ` ` `// Depending upon node position ` ` ` `// add value to their respective sum ` ` ` `if` `(oddPosition) ` ` ` `oddPositionNodeSum += node->data; ` ` ` `else` ` ` `evenPositionNodeSum += node->data; ` ` ` ` ` `q.pop(); ` ` ` `if` `(node->left != NULL) ` ` ` `q.push(node->left); ` ` ` `if` `(node->right != NULL) ` ` ` `q.push(node->right); ` ` ` `nodeCount--; ` ` ` ` ` `// Switch the even position flag ` ` ` `oddPosition = !oddPosition; ` ` ` `} ` ` ` `} ` ` ` ` ` `// Return the absolute difference ` ` ` `return` `abs` `(oddPositionNodeSum - evenPositionNodeSum); ` `} ` ` ` `// Utility method to create a node ` `struct` `Node* newNode(` `int` `data) ` `{ ` ` ` `struct` `Node* node = ` `new` `Node; ` ` ` `node->data = data; ` ` ` `node->left = node->right = NULL; ` ` ` `return` `(node); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `struct` `Node* root = newNode(1); ` ` ` `root->left = newNode(2); ` ` ` `root->right = newNode(3); ` ` ` `root->left->left = newNode(4); ` ` ` `root->left->right = newNode(5); ` ` ` `root->right->left = newNode(6); ` ` ` `root->right->right = newNode(7); ` ` ` `root->left->right->left = newNode(8); ` ` ` `root->left->right->right = newNode(9); ` ` ` `root->left->right->right->right = newNode(10); ` ` ` ` ` `cout << nodeSumDiff(root); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

7

## Recommended Posts:

- Difference between sums of odd level and even level nodes of a Binary Tree
- Swap Nodes in Binary tree of every k'th level
- Print nodes between two given level numbers of a binary tree
- Count nodes with two children at level L in a Binary Tree
- Print all the nodes except the leftmost node in every level of the given binary tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print extreme nodes of each level of Binary Tree in alternate order
- Recursive Program to Print extreme nodes of each level of Binary Tree in alternate order
- Count the number of nodes at given level in a tree using BFS.
- Sum of nodes at k-th level in a tree represented as string
- Level with maximum number of nodes using DFS in a N-ary tree
- Product of nodes at k-th level in a tree represented as string
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Largest value in each level of Binary Tree

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.