Replace each node in given N-ary Tree with sum of all its subtrees
Given an N-ary tree. The task is to replace the values of each node with the sum of all its subtrees and the node itself.
Examples
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. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.
Input: 1
/ | \
2 3 4
/ \\
5 67
Output: Initial Pre-order Traversal: 1 2 5 6 7 3 4
Final Pre-order Traversal: 28 20 5 6 7 3 4
Explanation: Value of each node is replaced by the sum of all its subtrees and the node itself.Input: 1
/ | \
4 2 3
/ \ \
7 6 5
Output: Initial Pre-order Traversal: 1 4 7 6 3 5
Final Pre-order Traversal: 23 13 7 6 28 5
Approach: This problem can be solved by using Recursion. Follow the steps below to solve the given problem.
- The easiest way to do this problem is by using recursion.
- Start with the base condition when the current node equals NULL then return 0, as it means it is the leaf node.
- Otherwise, make a recursion call to all its child nodes by traversing using a loop and add the sum of all child nodes in it.
- At last return the current node’s data.
- In this way, all the node’s values will be replaced by the sum of all the subtrees and itself.
Below is the implementation of the above approach.
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Class for the node of the treestruct Node { int data; // List of children struct Node** children; int length; Node() { length = 0; data = 0; } Node(int n, int data_) { children = new Node*(); length = n; data = data_; }}; // Function to replace node with// sum of its left subtree, right// subtree and its sumint sumReplacementNary(Node* node){ if (node == NULL) return 0; // Total children count int total = node->length; // Taking sum of all the nodes for (int i = 0; i < total; i++) node->data += sumReplacementNary(node->children[i]); return node->data;} void preorderTraversal(Node* node){ if (node == NULL) return; // Total children count int total = node->length; // Print the current node's data cout << node->data << " "; // All the children except the last for (int i = 0; i < total - 1; i++) preorderTraversal(node->children[i]); // Last child preorderTraversal(node->children[total - 1]);} // Driver codeint main(){ /* Create the following tree 1 / | \ 2 3 4 / \ \ 5 6 7 */ int N = 3; Node* root = new Node(N, 1); root->children[0] = new Node(N, 2); root->children[1] = new Node(N, 3); root->children[2] = new Node(N, 4); root->children[0]->children[0] = new Node(N, 5); root->children[0]->children[1] = new Node(N, 6); root->children[0]->children[2] = new Node(N, 7); cout << "Initial Pre-order Traversal: "; preorderTraversal(root); cout << endl; cout << "Final Pre-order Traversal: "; sumReplacementNary(root); preorderTraversal(root); return 0;} |
Initial Pre-order Travedsal: 1 2 5 6 7 3 4 Final Pre-order Traversal: 28 20 5 6 7 3 4
Time Complexity: O(N), Where N is the number of nodes in the tree.



