📜  N元树的所有元素的总和

📅  最后修改于: 2021-05-04 17:10:35             🧑  作者: Mango

给定一个N元树,找到其中的所有元素之和。

例子 :

Input : Above tree
Output : Sum is 536

方法:所使用的方法类似于二叉树中的“级别顺序”遍历。首先推送队列中的根节点。对于每个节点,将其从队列中弹出时,将该节点的值添加到sum变量中,然后将弹出元素的子级推入队列中。如果是通用树,则将子节点存储在向量中。因此,将向量的所有元素放入队列中。

下面是上述想法的实现:

C++
// C++ program to find sum of all
// elements in generic tree
#include 
using namespace std;
  
// Represents a node of an n-ary tree
struct Node {
    int key;
    vector child;
};
  
// Utility function to create a new tree node
Node* newNode(int key)
{
    Node* temp = new Node;
    temp->key = key;
    return temp;
}
  
// Function to compute the sum
// of all elements in generic tree
int sumNodes(Node* root)
{
    // initialize the sum variable
    int sum = 0;
  
    if (root == NULL)
        return 0;
  
    // Creating a queue and pushing the root
    queue q;
    q.push(root);
  
    while (!q.empty()) {
        int n = q.size();
  
        // If this node has children
        while (n > 0) {
  
            // Dequeue an item from queue and
            // add it to variable "sum"
            Node* p = q.front();
            q.pop();
            sum += p->key;
  
            // Enqueue all children of the dequeued item
            for (int i = 0; i < p->child.size(); i++)
                q.push(p->child[i]);
            n--;
        }
    }
    return sum;
}
  
// Driver program
int main()
{
    // Creating a generic tree
    Node* root = newNode(20);
    (root->child).push_back(newNode(2));
    (root->child).push_back(newNode(34));
    (root->child).push_back(newNode(50));
    (root->child).push_back(newNode(60));
    (root->child).push_back(newNode(70));
    (root->child[0]->child).push_back(newNode(15));
    (root->child[0]->child).push_back(newNode(20));
    (root->child[1]->child).push_back(newNode(30));
    (root->child[2]->child).push_back(newNode(40));
    (root->child[2]->child).push_back(newNode(100));
    (root->child[2]->child).push_back(newNode(20));
    (root->child[0]->child[1]->child).push_back(newNode(25));
    (root->child[0]->child[1]->child).push_back(newNode(50));
  
    cout << sumNodes(root) << endl;
  
    return 0;
}


Java
// Java program to find sum of all
// elements in generic tree
import java.util.*;
  
class GFG
{
  
// Represents a node of an n-ary tree
static class Node 
{
    int key;
    Vector child;
};
  
// Utility function to create a new tree node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
    temp.child = new Vector<>();
    return temp;
}
  
// Function to compute the sum
// of all elements in generic tree
static int sumNodes(Node root)
{
    // initialize the sum variable
    int sum = 0;
  
    if (root == null)
        return 0;
  
    // Creating a queue and pushing the root
    Queue q = new LinkedList<>();
    q.add(root);
  
    while (!q.isEmpty()) 
    {
        int n = q.size();
  
        // If this node has children
        while (n > 0)
        {
  
            // Dequeue an item from queue and
            // add it to variable "sum"
            Node p = q.peek();
            q.remove();
            sum += p.key;
  
            // Enqueue all children of the dequeued item
            for (int i = 0; i < p.child.size(); i++)
                q.add(p.child.get(i));
            n--;
        }
    }
    return sum;
}
  
// Driver program
public static void main(String[] args)
{
    // Creating a generic tree
    Node root = newNode(20);
    (root.child).add(newNode(2));
    (root.child).add(newNode(34));
    (root.child).add(newNode(50));
    (root.child).add(newNode(60));
    (root.child).add(newNode(70));
    (root.child.get(0).child).add(newNode(15));
    (root.child.get(0).child).add(newNode(20));
    (root.child.get(1).child).add(newNode(30));
    (root.child.get(2).child).add(newNode(40));
    (root.child.get(2).child).add(newNode(100));
    (root.child.get(2).child).add(newNode(20));
    (root.child.get(0).child.get(1).child).add(newNode(25));
    (root.child.get(0).child.get(1).child).add(newNode(50));
  
    System.out.print(sumNodes(root) +"\n");
}
}
  
// This code is contributed by 29AjayKumar


C#
// C# program to find sum of all
// elements in generic tree
using System;
using System.Collections.Generic;
  
class GFG
{
  
// Represents a node of an n-ary tree
class Node 
{
    public int key;
    public List child;
};
  
// Utility function to create a new tree node
static Node newNode(int key)
{
    Node temp = new Node();
    temp.key = key;
    temp.child = new List();
    return temp;
}
  
// Function to compute the sum
// of all elements in generic tree
static int sumNodes(Node root)
{
    // initialize the sum variable
    int sum = 0;
  
    if (root == null)
        return 0;
  
    // Creating a queue and pushing the root
    Queue q = new Queue();
    q.Enqueue(root);
  
    while (q.Count != 0) 
    {
        int n = q.Count;
  
        // If this node has children
        while (n > 0)
        {
  
            // Dequeue an item from queue and
            // add it to variable "sum"
            Node p = q.Peek();
            q.Dequeue();
            sum += p.key;
  
            // Enqueue all children of the dequeued item
            for (int i = 0; i < p.child.Count; i++)
                q.Enqueue(p.child[i]);
            n--;
        }
    }
    return sum;
}
  
// Driver program
public static void Main(String[] args)
{
    // Creating a generic tree
    Node root = newNode(20);
    (root.child).Add(newNode(2));
    (root.child).Add(newNode(34));
    (root.child).Add(newNode(50));
    (root.child).Add(newNode(60));
    (root.child).Add(newNode(70));
    (root.child[0].child).Add(newNode(15));
    (root.child[0].child).Add(newNode(20));
    (root.child[1].child).Add(newNode(30));
    (root.child[2].child).Add(newNode(40));
    (root.child[2].child).Add(newNode(100));
    (root.child[2].child).Add(newNode(20));
    (root.child[0].child[1].child).Add(newNode(25));
    (root.child[0].child[1].child).Add(newNode(50));
  
    Console.Write(sumNodes(root) +"\n");
}
}
  
// This code is contributed by PrinciRaj1992


输出:

536

时间复杂度: O(N),其中N是树中的节点数。
辅助空间: O(N),其中N是树中的节点数。