Sum of all elements of N-ary Tree
Given an N-ary tree, find sum of all elements in it.

Example :
Input : Above tree Output : Sum is 536
Approach : The approach used is similar to Level Order traversal in a binary tree. Start by pushing the root node in the queue. And for each node, while popping it from queue, add the value of this node in the sum variable and push the children of the popped element in the queue. In case of a generic tree store child nodes in a vector. Thus, put all elements of the vector in the queue.
Below is the implementation of the above idea :
// C++ program to find sum of all // elements in generic tree #include <bits/stdc++.h> using namespace std; // Represents a node of an n-ary tree struct Node { int key; vector<Node*> child; }; // Utility function to create a new tree node Node* newNode(int key) { Node* temp = new Node; temp->key = key; return temp; } // Function to compute the sum // of all elements in generic tree int sumNodes(Node* root) { // initialize the sum variable int sum = 0; if (root == NULL) return 0; // Creating a queue and pushing the root queue<Node*> q; q.push(root); while (!q.empty()) { int n = q.size(); // If this node has children while (n > 0) { // Dequeue an item from queue and // add it to variable "sum" Node* p = q.front(); q.pop(); sum += p->key; // Enqueue all children of the dequeued item for (int i = 0; i < p->child.size(); i++) q.push(p->child[i]); n--; } } return sum; } // Driver program int main() { // Creating a generic tree Node* root = newNode(20); (root->child).push_back(newNode(2)); (root->child).push_back(newNode(34)); (root->child).push_back(newNode(50)); (root->child).push_back(newNode(60)); (root->child).push_back(newNode(70)); (root->child[0]->child).push_back(newNode(15)); (root->child[0]->child).push_back(newNode(20)); (root->child[1]->child).push_back(newNode(30)); (root->child[2]->child).push_back(newNode(40)); (root->child[2]->child).push_back(newNode(100)); (root->child[2]->child).push_back(newNode(20)); (root->child[0]->child[1]->child).push_back(newNode(25)); (root->child[0]->child[1]->child).push_back(newNode(50)); cout << sumNodes(root) << endl; return 0; } |
Output:
536
Time Complexity : O(N), where N is the number of nodes in tree.
Auxiliary Space : O(N), where N is the number of nodes in tree.
Recommended Posts:
- Top three elements in binary tree
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST
- Complexity of different operations in Binary tree, Binary Search Tree and AVL tree
- Given level order traversal of a Binary Tree, check if the Tree is a Min-Heap
- Print Binary Tree levels in sorted order | Set 3 (Tree given as array)
- Convert an arbitrary Binary Tree to a tree that holds Children Sum Property
- Convert a given Binary tree to a tree that holds Logical AND property
- Check if a given Binary Tree is height balanced like a Red-Black Tree
- Convert a given Binary tree to a tree that holds Logical OR property
- Check whether a binary tree is a complete tree or not | Set 2 (Recursive Solution)
- Cartesian tree from inorder traversal | Segment Tree
- Sub-tree with minimum color difference in a 2-coloured tree
- Create a mirror tree from the given binary tree
- Check if the given binary tree has a sub-tree with equal no of 1's and 0's | Set 2
- Difference between General tree and 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.



