Given an N-ary Tree rooted at 1, the task is to find the difference between the sum of nodes at the odd level and the sum of nodes at even level.
/ | \
2 3 -5
/ \ / \
-1 3 -2 6
Sum of nodes at even levels = 2 + 3 + (-5) = 0
Sum of nodes at odd levels = 4 + (-1) + 3 + (-2) + 6 = 10
Hence, the required difference is 10.
/ | \
2 -1 3
/ \ \
4 5 8
/ / | \
2 6 12 7
Approach: To solve the problem, the idea is to find the respective sums of the nodes at the even and odd levels using Level Order Traversal and calculate the difference between them. Follow the steps below to solve the problem:
- Initialize a Queue to store nodes and their respective levels.
- Initialize variables evenSum and oddSum to store the sum of nodes at the even and odd levels respectively.
- Push the root of the N-ary Tree along with its corresponding level, i.e., 1, into the Queue.
- Now, iterate and repeat the following steps until the Queue becomes empty:
- Pop the nodes from the Queue. Store the level of the popped node in a variable, say currentLevel.
- If currentLevel is even, add the value of the node to evenSum. Otherwise, add to oddSum.
- Push all its children to the Queue and set their respective levels as currentLevel + 1.
- Once the above steps are completed, calculate and print the difference between oddSum and evenSum.
Below is the implementation of the above approach:
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.
- Difference between sums of odd level and even level nodes of a Binary Tree
- Difference between sums of odd position and even position nodes 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
- Difference between sum of even and odd valued nodes in a Binary Tree
- Print levels with odd number of nodes and even number of nodes
- Print nodes of a Binary Search Tree in Top Level Order and Reversed Bottom Level Order alternately
- Print the nodes corresponding to the level value for each level of a Binary Tree
- Count nodes from all lower levels smaller than minimum valued node of current level for every level in a Binary Tree
- Check if a Binary Tree is an Even-Odd Tree or not
- Queries to find the maximum Xor value between X and the nodes of a given level of a perfect binary tree
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Print all the levels with odd and even number of nodes in it | Set-2
- Connect Nodes at same Level (Level Order Traversal)
- Print nodes between two given level numbers of a binary tree
- Minimum difference between any two weighted nodes in Sum Tree of the given Tree
- Sum of all odd nodes in the path connecting two given nodes
- Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels
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.
Improved By : sanjeev2552