📜  N 叉树中的最大元素

📅  最后修改于: 2021-09-04 08:30:48             🧑  作者: Mango

鉴于由N个节点的N叉树,任务是寻找具有在指定N进制树最大值的节点。

例子:

方法:可以通过遍历给定的N叉树并跟踪出现的节点的最大值来解决给定的问题。遍历完成后,打印得到的最大值。

下面是上述方法的实现:

C++
// C++ program for the above approach
 
#include 
using namespace std;
 
// Structure of a
// node of N-ary tree
struct Node {
    int key;
    vector child;
};
 
// Stores the node with largest value
Node* maximum = NULL;
 
// Function to create a new Node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
 
    // Return the newly created node
    return temp;
}
 
// Function to find the node with
// largest value in N-ary tree
void findlargest(Node* root)
{
    // Base Case
    if (root == NULL)
        return;
 
    // If maximum is NULL, return
    // the value of root node
    if ((maximum) == NULL)
        maximum = root;
 
    // If value of the root is greater
    // than maximum, update the maximum node
    else if (root->key > (maximum)->key) {
        maximum = root;
    }
 
    // Recursively call for all the
    // children of the root node
    for (int i = 0;
         i < root->child.size(); i++) {
        findlargest(root->child[i]);
    }
}
 
// Driver Code
int main()
{
    // Given N-ary tree
    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);
 
    // Print the largest value
    cout << maximum->key;
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Structure of a
// node of N-ary tree
static class Node
{
    int key;
    Vector child = new Vector<>();
};
 
// Stores the node with largest value
static Node maximum = null;
 
// Function to create a new Node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
 
    // Return the newly created node
    return temp;
}
 
// Function to find the node with
// largest value in N-ary tree
static void findlargest(Node root)
{
     
    // Base Case
    if (root == null)
        return;
 
    // If maximum is null, return
    // the value of root node
    if ((maximum) == null)
        maximum = root;
 
    // If value of the root is greater
    // than maximum, update the maximum node
    else if (root.key > (maximum).key)
    {
        maximum = root;
    }
 
    // Recursively call for all the
    // children of the root node
    for(int i = 0;
            i < root.child.size(); i++)
    {
        findlargest(root.child.get(i));
    }
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given N-ary tree
    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);
 
    // Print the largest value
    System.out.print(maximum.key);
}
}
 
// This code is contributed by Princi Singh


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG{
 
// Structure of a
// node of N-ary tree
class Node
{
    public int key;
    public List child = new List();
};
 
// Stores the node with largest value
static Node maximum = null;
 
// Function to create a new Node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
 
    // Return the newly created node
    return temp;
}
 
// Function to find the node with
// largest value in N-ary tree
static void findlargest(Node root)
{
     
    // Base Case
    if (root == null)
        return;
 
    // If maximum is null, return
    // the value of root node
    if ((maximum) == null)
        maximum = root;
 
    // If value of the root is greater
    // than maximum, update the maximum node
    else if (root.key > (maximum).key)
    {
        maximum = root;
    }
 
    // Recursively call for all the
    // children of the root node
    for(int i = 0;
            i < root.child.Count; i++)
    {
        findlargest(root.child[i]);
    }
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given N-ary tree
    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);
 
    // Print the largest value
    Console.Write(maximum.key);
}
}
 
// This code is contributed by 29AjayKumar


输出:
90

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live