给定一个 N 元树,由值在[0, N – 1]范围内的节点和数组arr[] 组成,其中每个节点i与值arr[i]相关联,任务是打印与任何相关联的最大值给定N-ary Tree每一层的节点。
例子:
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.
方法:这个问题可以通过对给定树执行 Level Order Traversal 来解决。在遍历树的同时,分别处理每一层的节点。对于正在处理的每个级别,计算该级别中所有节点的最大值。请按照以下步骤操作:
- 将当前关卡的所有子节点存储在一个Queue中,将当前关卡的节点一一弹出。
- 找出当前关卡所有弹出节点的最大值。
- 打印上一步得到的最大值。
- 对给定 Tree 的每个级别执行上述步骤,并分别打印每个级别的最大值。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find the maximum value
// at each level of N-ary tree
int maxAtLevel(int N, int M,
vector Value,
int Edges[][2])
{
// Stores the adjacency list
vector adj[N];
// Create the adjacency list
for (int i = 0; i < M; i++) {
int u = Edges[i][0];
int v = Edges[i][1];
adj[u].push_back(v);
}
// Perform level order traversal
// of nodes at each level
queue q;
// Push the root node
q.push(0);
// Iterate until queue is empty
while (!q.empty()) {
// Get the size of queue
int count = q.size();
int maxVal = 0;
// Iterate for all the nodes
// in the queue currently
while (count--) {
// Dequeue an node from queue
int temp = q.front();
q.pop();
maxVal = max(maxVal,
Value[temp]);
// Enqueue the children of
// dequeued node
for (int i = 0;
i < adj[temp].size();
i++) {
q.push(adj[temp][i]);
}
}
// Print the result
cout << maxVal << " ";
}
}
// Driver Code
int main()
{
// Number of nodes
int N = 10;
// Edges of the N-ary tree
int Edges[][2] = { { 0, 1 }, { 0, 2 },
{ 0, 3 }, { 1, 4 },
{ 1, 5 }, { 3, 6 },
{ 6, 7 }, { 6, 8 },
{ 6, 9 } };
// Given cost
vector Value = { 1, 2, -1, 3, 4,
5, 8, 6, 12, 7 };
// Function Call
maxAtLevel(N, N - 1, Value, Edges);
return 0;
}
Java
// Java program for
// the above approach
import java.util.*;
class GFG{
// Function to find the maximum value
// at each level of N-ary tree
static void maxAtLevel(int N, int M,
int []Value,
int Edges[][])
{
// Stores the adjacency list
Vector []adj = new Vector[N];
for (int i = 0; i < adj.length; i++)
adj[i] = new Vector();
// Create the adjacency list
for (int i = 0; i < M; i++)
{
int u = Edges[i][0];
int v = Edges[i][1];
adj[u].add(v);
}
// Perform level order traversal
// of nodes at each level
Queue q = new LinkedList<>();
// Push the root node
q.add(0);
// Iterate until queue is empty
while (!q.isEmpty())
{
// Get the size of queue
int count = q.size();
int maxVal = 0;
// Iterate for all the nodes
// in the queue currently
while (count-- > 0)
{
// Dequeue an node from queue
int temp = q.peek();
q.remove();
maxVal = Math.max(maxVal, Value[temp]);
// Enqueue the children of
// dequeued node
for (int i = 0;
i < adj[temp].size(); i++)
{
q.add(adj[temp].get(i));
}
}
// Print the result
System.out.print(maxVal + " ");
}
}
// Driver Code
public static void main(String[] args)
{
// Number of nodes
int N = 10;
// Edges of the N-ary tree
int Edges[][] = {{0, 1}, {0, 2},
{0, 3}, {1, 4},
{1, 5}, {3, 6},
{6, 7}, {6, 8},
{6, 9}};
// Given cost
int []Value = {1, 2, -1, 3, 4,
5, 8, 6, 12, 7};
// Function Call
maxAtLevel(N, N - 1, Value, Edges);
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program for the above approach
# Function to find the maximum value
# at each level of N-ary tree
def maxAtLevel(N, M, Value, Edges):
# Stores the adjacency list
adj = [[] for i in range(N)]
# Create the adjacency list
for i in range(M):
u = Edges[i][0]
v = Edges[i][1]
adj[u].append(v)
# Perform level order traversal
# of nodes at each level
q = []
# Push the root node
q.append(0)
# Iterate until queue is empty
while (len(q)):
# Get the size of queue
count = len(q)
maxVal = 0
# Iterate for: all the nodes
# in the queue currently
while (count):
# Dequeue an node from queue
temp = q[0]
q.remove(q[0])
maxVal = max(maxVal, Value[temp])
# Enqueue the children of
# dequeued node
for i in range(len(adj[temp])):
q.append(adj[temp][i])
count -= 1
# Print the result
print(maxVal, end = " ")
# Driver Code
if __name__ == '__main__':
# Number of nodes
N = 10
# Edges of the N-ary tree
Edges = [ [ 0, 1 ], [ 0, 2 ],
[ 0, 3 ], [ 1, 4 ],
[ 1, 5 ], [ 3, 6 ],
[ 6, 7 ], [ 6, 8 ],
[ 6, 9 ] ]
# Given cost
Value = [ 1, 2, -1, 3, 4,
5, 8, 6, 12, 7 ]
# Function Call
maxAtLevel(N, N - 1, Value, Edges)
# This code is contributed by ipg2016107
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to find the
// maximum value at each
// level of N-ary tree
static void maxAtLevel(int N, int M,
int []Value,
int [,]Edges)
{
// Stores the adjacency list
List []adj = new List[N];
for (int i = 0; i < adj.Length; i++)
adj[i] = new List();
// Create the adjacency list
for (int i = 0; i < M; i++)
{
int u = Edges[i, 0];
int v = Edges[i, 1];
adj[u].Add(v);
}
// Perform level order traversal
// of nodes at each level
Queue q = new Queue();
// Push the root node
q.Enqueue(0);
// Iterate until queue is empty
while (q.Count != 0)
{
// Get the size of queue
int count = q.Count;
int maxVal = 0;
// Iterate for all the nodes
// in the queue currently
while (count-- > 0)
{
// Dequeue an node from queue
int temp = q.Peek();
q.Dequeue();
maxVal = Math.Max(maxVal,
Value[temp]);
// Enqueue the children of
// dequeued node
for (int i = 0;
i < adj[temp].Count; i++)
{
q.Enqueue(adj[temp][i]);
}
}
// Print the result
Console.Write(maxVal + " ");
}
}
// Driver Code
public static void Main(String[] args)
{
// Number of nodes
int N = 10;
// Edges of the N-ary tree
int [,]Edges = {{0, 1}, {0, 2},
{0, 3}, {1, 4},
{1, 5}, {3, 6},
{6, 7}, {6, 8},
{6, 9}};
// Given cost
int []Value = {1, 2, -1, 3, 4,
5, 8, 6, 12, 7};
// Function Call
maxAtLevel(N, N - 1, Value, Edges);
}
}
// This code is contributed by 29AjayKumar
1 3 8 12
时间复杂度: O(N)
辅助空间: O(N)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live