Maximum level sum in 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, organizational 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 a tree with N nodes numbered from 0 to N-1 and an array A[] containing the values of each node i.e. A[i] depict the value of the ith node. The relation between the nodes is given a two-dimensional array edges[][]. The task is to find the maximum level sum of the tree.

Sample Example 1

Input

N = 8
A[] = {1, 7, 8, 9, 5, 2, 3, 4}
edges[][] = {{0, 1}, {0, 2}, {0, 3}, {2, 4}, {3, 5}, {5, 6}, {5, 7}}

Output

24

Explanation

  • The sum of level 0: 1

  • The sum of level 1: 7+8+9 = 24

  • The sum of level 2: 5+2 = 7

  • The sum of level 3: 3+4 = 7

  • The maximum sum is 24 i.e. level 1.

Sample Example 2

Input

N = 3
A[] = {1, 3, 2}
edges[][] = {{0, 1}, {1, 2}}

Output

3

Explanation

  • The sum of level 0: 1

  • The sum of level 1: 3

  • The sum of level 2: 1

  • The maximum sum is 3 i.e. level 1.

Solution Approach

The problem can be solved by doing a level order traversal of the tree and storing the sum of every level and picking the maximum sum at the end and deciding the level with the maximum sum.

Pseudocode

function maximumLevelSum(N, edges, A):
   adj[N]
   for i from 0 to (N - 1):
      adj[edges[i][0]].push_back(edges[i][1])
   maxLevelSum = A[0]
   queue q
   q.push(0)
   while q is not empty:
      count = q.size()
      sum = 0
      while count > 0:
         node = q.front()
         q.pop()
         sum += A[node]
         for i from 0 to size of adj[node]:
            q.push(adj[node][i])
         count = count - 1
      maxLevelSum = max(maxLevelSum, sum)
   return maxLevelSum

Example: C++ Implementation

The following program does the level order traversal o the N-ary tree to get the maximum level sum.

#include <bits/stdc++.h>
using namespace std;

// Function to find the maximum level sum in N-ary tree
int maximumLevelSum(int N, int edges[][2], vector<int> A){
   // Creating the adjacency list representation for the tree
   vector<int> adj[N];
   for (int i = 0; i < (N - 1); i++){
      adj[edges[i][0]].push_back(edges[i][1]);
   }
   // Initialize the maximum level sum as the val[0] which is the level sum for level 0
   int maxLevelSum = A[0];
   // Creating a queue to store the nodes of each level for performing the level order traversal
   queue<int> q;
   q.push(0);
   // level order traversal
   while (!q.empty()){
      int count = q.size();
      int sum = 0;
      while (count--) {
         int node = q.front();
         q.pop();
         sum += A[node];
         for (int i = 0; i < adj[node].size(); i++) {
            q.push(adj[node][i]);
         }
      }
      // Update maximum level sum
      maxLevelSum = max(maxLevelSum, sum);
   }
   // Return the maximum level order sum
   return maxLevelSum;
}
int main(){
   int N = 8;
   int edges[][2] = {{0, 1}, {0, 2}, {0, 3}, {2, 4}, {3, 5}, {5, 6}, {5, 7}};
   vector<int> A = {1, 7, 8, 9, 5, 2, 3, 4};
   cout << maximumLevelSum(N, edges, A);
   return 0;
}

Output

24

Conclusion

In conclusion, an N-ary tree is a tree data structure where each node can have up to N children. The given C++ code shows how to find the maximum level sum in an N-ary tree. It uses an adjacency list representation for the tree and performs a level-order traversal using a queue. The maximum level sum is updated and the final result is returned with a time complexity of O(N).

Updated on: 03-Nov-2023

129 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements