- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between sums of odd level and even level nodes in an N-ary Tree
The N-ary tree is a tree data structure where each node can have a maximum of N children where N is a positive integer (N >= 0). N-ary trees are used in many applications like file systems, organisational charts and syntax trees in programming languages.
Example of N-ary tree with N = 4.
A / / \ \ B C D E / | \ | | \ F G H I J K | | L M
Problem Statement
Given an N-ary tree where the level of the root node is assumed to be 0. Find the absolute difference between the level sum of odd levels and even levels.
Sample Example 1
Input
10 -> Level 0 / | \ -5 20 15 -> Level 1 / \ / | \ -10 -3 0 8 -7 -> Level 2
Output
32
Explanation
Level Sum of even levels = 10 + (-10) + (-3) + 0 + 8 + (-7) = -2
Level Sum of odd levels = (-5) + 20 + 15 = 30
Absolute difference = |30 - (-2)| = 32
Sample Example 2
Input
50 -> Level 0 / | \ 25 -30 45 -> Level 1 / \ / | \ -15 10 20 5 35 -> Level 2 / \ / | \ -20 -10 15 40 -5 -> Level 3
Output
45
Explanation
Level Sum of even levels = 50 + (-15) + 10 + 20+ 5 + 35 = 105
Level Sum of odd levels = 25 + (-30) + 45 + (-20) + (-10) + 15 + 40 + (-5) = 60
Absolute difference = |60 - 105| = 45
Solution: Level Order Traversal
The problem can be solved by doing a level order traversal of the tree and storing the sum of odd and even levels separately and then lastly finding the absolute difference.
Pseudocode
function evenOddDiff(root: Node) -> integer even <- 0 odd <- 0 queue <- new Queue of (Node, integer) pairs queue.push((root, 0)) while queue is not empty curr, level <- queue.pop() data <- curr.val if level is even even <- even + data else odd <- odd + data for c in curr.child queue.push((c, level + 1)) return abs(odd - even)
Example: C++ Implementation
The following program does the level order traversal o the N-ary tree to get the difference between the odd and even level sum of the N-ary tree.
#include <bits/stdc++.h> using namespace std; // Structure of node in the N-ary tree struct Node { int val; vector<Node *> child; }; // Creating a new node in the N-ary tree Node *newNode(int val) { Node *temp = new Node; temp->val = val; return temp; } // Function to find the difference bewtween the odd and even level sum of nodes in the N-ary tree int evenOddDiff(Node *root) { int even = 0, odd = 0; // Queue of pair storing node value and level queue<pair<Node *, int>> q; q.push({root, 0}); while (!q.empty()) { pair<Node *, int> curr = q.front(); q.pop(); int level = curr.second; int data = curr.first->val; // Checking level and adding the value of node to sum if (level % 2) odd += data; else even += data; // Adding new nodes to queue along with updated level for (auto c : curr.first->child) { q.push({c, level + 1}); } } return abs(odd - even); } int main(){ Node *root = newNode(50); root->child.push_back(newNode(25)); root->child.push_back(newNode(-30)); root->child.push_back(newNode(45)); root->child[0]->child.push_back(newNode(-15)); root->child[0]->child.push_back(newNode(10)); root->child[2]->child.push_back(newNode(20)); root->child[2]->child.push_back(newNode(5)); root->child[2]->child.push_back(newNode(35)); root->child[0]->child[0]->child.push_back(newNode(-20)); root->child[0]->child[0]->child.push_back(newNode(-10)); root->child[2]->child[2]->child.push_back(newNode(15)); root->child[2]->child[2]->child.push_back(newNode(40)); root->child[2]->child[2]->child.push_back(newNode(-5)); cout << evenOddDiff(root); return 0; }
Output
45
Time Complexity − O(N) as while traversing the N-ary tree, we go through al the nodes of the tree.
Space Complexity − O(N) as all the nodes in the tree are stored in the queue data structure used.
Conclusion
In conclusion, the provided solution helps find the absolute difference between the odd and even level sums of the N-ary tree. The code has a time and space complexity of O(N) where N is the total number of nodes in the N-ary tree. Using the BFS or Level order traversal we calculated the sum of odd and even levels of the N-ary tree simultaneously. The absolute difference is obtained by subtracting the two sums calculated.