Maximum value at each level in an N-ary Tree
Last Updated :
22 Jun, 2021
Given a N-ary Tree consisting of nodes valued in the range [0, N – 1] and an array arr[] where each node i is associated to value arr[i], the task is to print the maximum value associated with any node at each level of the given N-ary Tree.
Examples:
Input: N = 8, Edges[][] = {{0, 1}, {0, 2}, {0, 3}, {1, 4}, {1, 5}, {3, 6}, {6, 7}},
arr[] = {4, 2, 3, -5, -1, 3, -2, 6}
Output: 4 3 6
Explanation:
Below is the given N-ary Tree:
The Max of all nodes of the 0th level is 4.
The Max of all nodes of the 1st level is 3.
The Max of all nodes of the 2nd level is 6.
Input: N = 10, Edges[][] = {{0, 1}, {0, 2}, {0, 3}, {1, 4}, {1, 5}, {3, 6}, {6, 7}, {6, 8}, {6, 9}},
arr[] = {1, 2, -1, 3, 4, 5, 8, 6, 12, 7}
Output: 1 3 8 12
Explanation:
Below is the given N-ary Tree:
The Max of all nodes of the 0th level is 1.
The Max of all nodes of the 1st level is 3.
The Max of all nodes of the 2nd level is 8.
The Max of all nodes of the 3rd level is 12.
Approach: This problem can be solved by performing the Level Order Traversal of the given tree. While traversing the tree, process nodes of each level separately. For every level being processed, compute the maximum value of all nodes in the level. Follow the steps below:
- Store all the child nodes of the current level in a Queue and pop the nodes of the current level one by one.
- Find the maximum value of all the popped nodes of the current level.
- Print the maximum value obtained in the above step.
- Follow the above steps for each level of the given Tree and print the maximum value for each level respectively.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxAtLevel( int N, int M,
vector< int > Value,
int Edges[][2])
{
vector< int > adj[N];
for ( int i = 0; i < M; i++) {
int u = Edges[i][0];
int v = Edges[i][1];
adj[u].push_back(v);
}
queue< int > q;
q.push(0);
while (!q.empty()) {
int count = q.size();
int maxVal = 0;
while (count--) {
int temp = q.front();
q.pop();
maxVal = max(maxVal,
Value[temp]);
for ( int i = 0;
i < adj[temp].size();
i++) {
q.push(adj[temp][i]);
}
}
cout << maxVal << " " ;
}
}
int main()
{
int N = 10;
int Edges[][2] = { { 0, 1 }, { 0, 2 },
{ 0, 3 }, { 1, 4 },
{ 1, 5 }, { 3, 6 },
{ 6, 7 }, { 6, 8 },
{ 6, 9 } };
vector< int > Value = { 1, 2, -1, 3, 4,
5, 8, 6, 12, 7 };
maxAtLevel(N, N - 1, Value, Edges);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maxAtLevel( int N, int M,
int []Value,
int Edges[][])
{
Vector<Integer> []adj = new Vector[N];
for ( int i = 0 ; i < adj.length; i++)
adj[i] = new Vector<Integer>();
for ( int i = 0 ; i < M; i++)
{
int u = Edges[i][ 0 ];
int v = Edges[i][ 1 ];
adj[u].add(v);
}
Queue<Integer> q = new LinkedList<>();
q.add( 0 );
while (!q.isEmpty())
{
int count = q.size();
int maxVal = 0 ;
while (count-- > 0 )
{
int temp = q.peek();
q.remove();
maxVal = Math.max(maxVal, Value[temp]);
for ( int i = 0 ;
i < adj[temp].size(); i++)
{
q.add(adj[temp].get(i));
}
}
System.out.print(maxVal + " " );
}
}
public static void main(String[] args)
{
int N = 10 ;
int Edges[][] = {{ 0 , 1 }, { 0 , 2 },
{ 0 , 3 }, { 1 , 4 },
{ 1 , 5 }, { 3 , 6 },
{ 6 , 7 }, { 6 , 8 },
{ 6 , 9 }};
int []Value = { 1 , 2 , - 1 , 3 , 4 ,
5 , 8 , 6 , 12 , 7 };
maxAtLevel(N, N - 1 , Value, Edges);
}
}
|
Python3
def maxAtLevel(N, M, Value, Edges):
adj = [[] for i in range (N)]
for i in range (M):
u = Edges[i][ 0 ]
v = Edges[i][ 1 ]
adj[u].append(v)
q = []
q.append( 0 )
while ( len (q)):
count = len (q)
maxVal = 0
while (count):
temp = q[ 0 ]
q.remove(q[ 0 ])
maxVal = max (maxVal, Value[temp])
for i in range ( len (adj[temp])):
q.append(adj[temp][i])
count - = 1
print (maxVal, end = " " )
if __name__ = = '__main__' :
N = 10
Edges = [ [ 0 , 1 ], [ 0 , 2 ],
[ 0 , 3 ], [ 1 , 4 ],
[ 1 , 5 ], [ 3 , 6 ],
[ 6 , 7 ], [ 6 , 8 ],
[ 6 , 9 ] ]
Value = [ 1 , 2 , - 1 , 3 , 4 ,
5 , 8 , 6 , 12 , 7 ]
maxAtLevel(N, N - 1 , Value, Edges)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static void maxAtLevel( int N, int M,
int []Value,
int [,]Edges)
{
List< int > []adj = new List< int >[N];
for ( int i = 0; i < adj.Length; i++)
adj[i] = new List< int >();
for ( int i = 0; i < M; i++)
{
int u = Edges[i, 0];
int v = Edges[i, 1];
adj[u].Add(v);
}
Queue< int > q = new Queue< int >();
q.Enqueue(0);
while (q.Count != 0)
{
int count = q.Count;
int maxVal = 0;
while (count-- > 0)
{
int temp = q.Peek();
q.Dequeue();
maxVal = Math.Max(maxVal,
Value[temp]);
for ( int i = 0;
i < adj[temp].Count; i++)
{
q.Enqueue(adj[temp][i]);
}
}
Console.Write(maxVal + " " );
}
}
public static void Main(String[] args)
{
int N = 10;
int [,]Edges = {{0, 1}, {0, 2},
{0, 3}, {1, 4},
{1, 5}, {3, 6},
{6, 7}, {6, 8},
{6, 9}};
int []Value = {1, 2, -1, 3, 4,
5, 8, 6, 12, 7};
maxAtLevel(N, N - 1, Value, Edges);
}
}
|
Javascript
<script>
function maxAtLevel(N, M, Value, Edges)
{
let adj = new Array(N);
for (let i = 0; i < adj.length; i++)
adj[i] = [];
for (let i = 0; i < M; i++)
{
let u = Edges[i][0];
let v = Edges[i][1];
adj[u].push(v);
}
let q = [];
q.push(0);
while (q.length > 0)
{
let count = q.length;
let maxVal = 0;
while (count-- > 0)
{
let temp = q[0];
q.shift();
maxVal = Math.max(maxVal, Value[temp]);
for (let i = 0; i < adj[temp].length; i++)
{
q.push(adj[temp][i]);
}
}
document.write(maxVal + " " );
}
}
let N = 10;
let Edges = [ [ 0, 1 ], [ 0, 2 ],
[ 0, 3 ], [ 1, 4 ],
[ 1, 5 ], [ 3, 6 ],
[ 6, 7 ], [ 6, 8 ],
[ 6, 9 ] ];
let Value = [ 1, 2, -1, 3, 4,
5, 8, 6, 12, 7 ];
maxAtLevel(N, N - 1, Value, Edges);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...