Largest element in an N-ary Tree
Last Updated :
27 Mar, 2023
Given an N-ary tree consisting of N nodes, the task is to find the node having the largest value in the given N-ary Tree.
Examples:
Input:
Output: 90
Explanation: The node with the largest value in the tree is 90.
Input:
Output: 95
Explanation: The node with the largest value in the tree is 95.
Approach: The given problem can be solved by traversing the given N-ary tree and keeping track of the maximum value of nodes that occurred. After completing the traversal, print the maximum value obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int key;
vector<Node*> child;
};
Node* maximum = NULL;
Node* newNode( int key)
{
Node* temp = new Node;
temp->key = key;
return temp;
}
void findlargest(Node* root)
{
if (root == NULL)
return ;
if ((maximum) == NULL)
maximum = root;
else if (root->key > (maximum)->key) {
maximum = root;
}
for ( int i = 0;
i < root->child.size(); i++) {
findlargest(root->child[i]);
}
}
int main()
{
Node* root = newNode(11);
(root->child).push_back(newNode(21));
(root->child).push_back(newNode(29));
(root->child).push_back(newNode(90));
(root->child[0]->child).push_back(newNode(18));
(root->child[1]->child).push_back(newNode(10));
(root->child[1]->child).push_back(newNode(12));
(root->child[2]->child).push_back(newNode(77));
findlargest(root);
cout << maximum->key;
return 0;
}
|
Java
import java.util.*;
class GFG{
static class Node
{
int key;
Vector<Node> child = new Vector<>();
};
static Node maximum = null ;
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
return temp;
}
static void findlargest(Node root)
{
if (root == null )
return ;
if ((maximum) == null )
maximum = root;
else if (root.key > (maximum).key)
{
maximum = root;
}
for ( int i = 0 ;
i < root.child.size(); i++)
{
findlargest(root.child.get(i));
}
}
public static void main(String[] args)
{
Node root = newNode( 11 );
(root.child).add(newNode( 21 ));
(root.child).add(newNode( 29 ));
(root.child).add(newNode( 90 ));
(root.child.get( 0 ).child).add(newNode( 18 ));
(root.child.get( 1 ).child).add(newNode( 10 ));
(root.child.get( 1 ).child).add(newNode( 12 ));
(root.child.get( 2 ).child).add(newNode( 77 ));
findlargest(root);
System.out.print(maximum.key);
}
}
|
Python3
class Node:
def __init__( self , key):
self .key = key
self .child = []
maximum = None
def newNode(key):
temp = Node(key)
return temp
def findlargest(root):
global maximum
if (root = = None ):
return
if ((maximum) = = None ):
maximum = root
elif (root.key > (maximum).key):
maximum = root
for i in range ( len (root.child)):
findlargest(root.child[i])
root = newNode( 11 )
(root.child).append(newNode( 21 ))
(root.child).append(newNode( 29 ))
(root.child).append(newNode( 90 ))
(root.child[ 0 ].child).append(newNode( 18 ))
(root.child[ 1 ].child).append(newNode( 10 ))
(root.child[ 1 ].child).append(newNode( 12 ))
(root.child[ 2 ].child).append(newNode( 77 ))
findlargest(root)
print (maximum.key)
|
C#
using System;
using System.Collections.Generic;
public class GFG{
class Node
{
public int key;
public List<Node> child = new List<Node>();
};
static Node maximum = null ;
static Node newNode( int key)
{
Node temp = new Node();
temp.key = key;
return temp;
}
static void findlargest(Node root)
{
if (root == null )
return ;
if ((maximum) == null )
maximum = root;
else if (root.key > (maximum).key)
{
maximum = root;
}
for ( int i = 0;
i < root.child.Count; i++)
{
findlargest(root.child[i]);
}
}
public static void Main(String[] args)
{
Node root = newNode(11);
(root.child).Add(newNode(21));
(root.child).Add(newNode(29));
(root.child).Add(newNode(90));
(root.child[0].child).Add(newNode(18));
(root.child[1].child).Add(newNode(10));
(root.child[1].child).Add(newNode(12));
(root.child[2].child).Add(newNode(77));
findlargest(root);
Console.Write(maximum.key);
}
}
|
Javascript
<script>
class Node
{
constructor(key) {
this .key = key;
this .child = [];
}
}
let maximum = null ;
function newNode(key)
{
let temp = new Node(key);
return temp;
}
function findlargest(root)
{
if (root == null )
return ;
if ((maximum) == null )
maximum = root;
else if (root.key > (maximum).key)
{
maximum = root;
}
for (let i = 0; i < root.child.length; i++)
{
findlargest(root.child[i]);
}
}
let root = newNode(11);
(root.child).push(newNode(21));
(root.child).push(newNode(29));
(root.child).push(newNode(90));
(root.child[0].child).push(newNode(18));
(root.child[1].child).push(newNode(10));
(root.child[1].child).push(newNode(12));
(root.child[2].child).push(newNode(77));
findlargest(root);
document.write(maximum.key);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N) In the worst case, if the tree is a skewed tree, then the space complexity can be O(n) due to function call stack.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...