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.
Input: 5 / \ 2 6 / \ \ 1 4 8 / / \ 3 7 9 Output: 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| = 11 Input: 5 / \ 2 3 Output: 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:
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.
- Difference between sums of odd level and even level nodes of a Binary Tree
- Difference between sums of odd level and even level nodes in an N-ary Tree
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Print odd positioned nodes of odd levels in level order of the given binary tree
- Print even positioned nodes of odd levels in level order of the given binary tree
- Print odd positioned nodes of even levels in level order of the given binary tree
- Print even positioned nodes of even levels in level order of the given binary tree
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Difference between sum of even and odd valued nodes in a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a 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
- Print the middle nodes of each level of a Binary Tree
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Print levels with odd number of nodes and even number of nodes
- Print nodes between two given level numbers of a binary tree
- Connect Nodes at same Level (Level Order Traversal)
- Check if a Binary Tree is an Even-Odd Tree or not
- Depth of the deepest odd level node in Binary Tree
- Height of binary tree considering even level leaves only
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.