# Maximum absolute difference between any two level sum in a Binary Tree

Given a Binary Tree having positive and negative nodes, the task is to find the maximum absolute difference of level sum in it.

Examples:

```Input:
4
/   \
2    -5
/  \   / \
-1    3 -2  6
Output: 9
Explanation:
Sum of all nodes of 0 level is 4
Sum of all nodes of 1 level is -3
Sum of all nodes of 2 level is 6
Hence maximum absolute difference
of level sum = 9 (6 - (-3))

Input:
1
/   \
2     3
/  \     \
4    5     8
/ \
6   7
Output: 16

```

Approach: To find the maximum absolute difference of level sum, we only need to find Maximum level sum and Minimum level sum because absolute difference of maximum and minimum level sum always gives us Maximum absolute difference, i.e.

Maximum absolute difference = abs(Maximum level sum – Minimum level sum)

Below are the steps for algorithm of above observation:

1. The idea is to do level order traversal of the tree.
2. While doing traversal, process nodes of different levels separately.
3. For every level being processed, compute sum of nodes in the level and keep track of maximum and minimum level sum.
4. Then return the absolute difference of maximum and minimum level sum.

Below is the implementation of the above approach:

 `// C++ program to find the maximum` `// absolute difference of level` `// sum in Binary Tree` `#include ` `using` `namespace` `std;`   `// Class containing left and` `// right child of current` `// node and key value` `struct` `Node` `{` `    ``int` `data;` `    ``Node *left, *right;` `};`   `Node *newNode(``int` `data) ` `{` `    ``Node *node = ``new` `Node();` `    ``node->data = data;` `    ``node->left = NULL;` `    ``node->right = NULL;` `    ``return` `node;` `}`   `// Function to find the maximum` `// absolute difference of level` `// sum in binary tree` `// using level order traversal` `int` `maxAbsDiffLevelSum(Node *root)` `{` `    `  `    ``// Initialize value of maximum` `    ``// and minimum level sum` `    ``int` `maxsum = INT_MIN;` `    ``int` `minsum = INT_MAX;` `    `  `    ``queue qu;` `    ``qu.push(root);` `    `  `    ``// Do Level order traversal` `    ``// keeping track of number` `    ``// of nodes at every level.` `    ``while` `(!qu.empty()) ` `    ``{` `        `  `        ``// Get the size of queue when` `        ``// the level order traversal` `        ``// for one level finishes` `        ``int` `sz = qu.size();` `        `  `        ``// Iterate for all the nodes in` `        ``// the queue currently` `        ``int` `sum = 0;` `        `  `        ``for``(``int` `i = 0; i < sz; i++)` `        ``{` `            `  `            ``// Dequeue an node from queue` `            ``Node *t = qu.front();` `            ``qu.pop();` `            `  `            ``// Add this node's value to` `            ``// the current sum.` `            ``sum += t->data;` `            `  `            ``// Enqueue left and` `            ``// right children of` `            ``// dequeued node` `            ``if` `(t->left != NULL) ` `                ``qu.push(t->left);` `            `  `            ``if` `(t->right != NULL)` `                ``qu.push(t->right);` `        ``}` `        `  `        ``// Update the maximum` `        ``// level sum value` `        ``maxsum = max(maxsum, sum);` `        `  `        ``// Update the minimum` `        ``// level sum value` `        ``minsum = min(minsum, sum);` `    ``}` `    `  `    ``// return the maximum absolute` `    ``// difference of level sum` `    ``return` `abs``(maxsum - minsum);` `}`   `// Driver code ` `int` `main() ` `{` `    ``Node *root = ``new` `Node();` `    `  `    ``root = newNode(4);` `    ``root->left = newNode(2);` `    ``root->right = newNode(-5);` `    ``root->left->left = newNode(-1);` `    ``root->left->right = newNode(3);` `    ``root->right->left = newNode(-2);` `    ``root->right->right = newNode(6);` `    `  `    ``/*   Constructed Binary tree is:` `        ``4` `      ``/   \` `    ``2      -5` `    ``/  \     / \` `    ``-1    3  -2   6 */` `    `  `    ``cout << maxAbsDiffLevelSum(root) << endl;` `}`   `// This code is contributed by sanjeev2552`

 `// Java program to find the maximum` `// absolute difference of level` `// sum in Binary Tree`   `import` `java.util.*;`   `// Class containing left and` `// right child of current` `// node and key value` `class` `Node {`   `    ``int` `data;` `    ``Node left, right;`   `    ``public` `Node(``int` `item)` `    ``{` `        ``data = item;` `        ``left = right = ``null``;` `    ``}` `}`   `class` `BinaryTree {`   `    ``// Root of the Binary Tree` `    ``Node root;`   `    ``public` `BinaryTree()` `    ``{` `        ``root = ``null``;` `    ``}`   `    ``// Function to find` `    ``// the maximum absolute` `    ``// difference of level` `    ``// sum in binary tree` `    ``// using level order traversal` `    ``public` `int` `maxAbsDiffLevelSum()` `    ``{`   `        ``// Initialize value of maximum` `        ``// and minimum level sum` `        ``int` `maxsum = Integer.MIN_VALUE;` `        ``int` `minsum = Integer.MAX_VALUE;`   `        ``Queue qu = ``new` `LinkedList<>();` `        ``qu.offer(root);`   `        ``// Do Level order traversal` `        ``// keeping track of number` `        ``// of nodes at every level.` `        ``while` `(!qu.isEmpty()) {`   `            ``// Get the size of queue when` `            ``// the level order traversal` `            ``// for one level finishes` `            ``int` `sz = qu.size();`   `            ``// Iterate for all the nodes in` `            ``// the queue currently` `            ``int` `sum = ``0``;`   `            ``for` `(``int` `i = ``0``; i < sz; i++) {`   `                ``// Dequeue an node from queue` `                ``Node t = qu.poll();`   `                ``// Add this node's value to` `                ``// the current sum.` `                ``sum += t.data;`   `                ``// Enqueue left and` `                ``// right children of` `                ``// dequeued node` `                ``if` `(t.left != ``null``)` `                    ``qu.offer(t.left);`   `                ``if` `(t.right != ``null``)` `                    ``qu.offer(t.right);` `            ``}`   `            ``// Update the maximum` `            ``// level sum value` `            ``maxsum = Math.max(maxsum, sum);`   `            ``// Update the minimum` `            ``// level sum value` `            ``minsum = Math.min(minsum, sum);` `        ``}` `        ``// return the maximum absolute` `        ``// difference of level sum` `        ``return` `Math.abs(maxsum - minsum);` `    ``}`   `    ``// Driver code` `    ``public` `static` `void` `main(String[] args)` `    ``{` `        ``BinaryTree tree = ``new` `BinaryTree();` `        ``tree.root = ``new` `Node(``4``);` `        ``tree.root.left = ``new` `Node(``2``);` `        ``tree.root.right = ``new` `Node(-``5``);` `        ``tree.root.left.left = ``new` `Node(-``1``);` `        ``tree.root.left.right = ``new` `Node(``3``);` `        ``tree.root.right.left = ``new` `Node(-``2``);` `        ``tree.root.right.right = ``new` `Node(``6``);`   `        ``/*   Constructed Binary tree is:` `              ``4` `            ``/   \` `          ``2      -5` `        ``/  \     / \` `      ``-1    3  -2   6 */`   `        ``System.out.println(` `            ``tree.maxAbsDiffLevelSum());` `    ``}` `}`

Output:
```9

```

Time Complexity: O(N)
Auxiliary Space: O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

Check out this Author's contributed articles.

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.

Improved By : sanjeev2552

Article Tags :
Practice Tags :