📌  相关文章
📜  N元树中奇数级和偶数级节点之和之间的差异

📅  最后修改于: 2021-04-22 08:39:10             🧑  作者: Mango

给定一个以1为根的N元树,任务是找到奇数级节点总数和偶数级节点总数之差。

例子:

方法:解决该问题的想法是使用“级别顺序遍历”找到偶数和奇数级别的各个节点之和,并计算它们之间的差。请按照以下步骤解决问题:

  • 初始化队列以存储节点及其各自的级别。
  • 初始化变量evenSumoddSum,以分别存储偶数和奇数级别的节点总和。
  • N元树及其相应级别(即1)推入Queue 
  • 现在,迭代并重复以下步骤,直到队列为空:
    • 从队列中弹出节点。将弹出节点的级别存储在一个变量中,例如currentLevel。
    • 如果currentLevel偶数,则将该节点的值添加到evenSum中。否则,添加到oddSum
    • 将其所有子级推入Queue并将它们各自的级别设置为currentLevel + 1
  • 完成上述步骤后,计算并打印oddSumevenSum之间的差异。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Structure of a node
// of an n-ary tree
struct Node {
    int val;
    vector children;
};
 
// Function to create a
// new tree node
Node* newNode(int val)
{
    Node* temp = new Node;
    temp->val = val;
    return temp;
}
 
// Function to find the difference
// between of sums node values of
// odd and even levels in an N-ary tree
int evenOddLevelDifference(Node* root)
{
    // Store the sums of nodes at
    // even and odd levels
    int evenSum = 0, oddSum = 0;
 
    // Initialize a queue to store
    // pair of node and level
    queue > q;
 
    // Push the root into the
    // queue with level 1
    q.push({ root, 1 });
 
    // Iterate all levels
    // of tree are traversed
    while (!q.empty()) {
 
        // Store the node at the
        // front of the queue
        pair currNode
            = q.front();
 
        // Pop the front node
        q.pop();
 
        // Store the current level
        int currLevel
            = currNode.second;
 
        // Store the current node value
        int currVal
            = currNode.first->val;
 
        // If current node
        // level is odd
        if (currLevel % 2)
 
            // Add to odd sum
            oddSum += currVal;
        else
 
            // Add to even sum
            evenSum += currVal;
 
        // Push all the children of current node
        // with increasing current level by 1
        for (auto child : currNode.first->children) {
            q.push({ child, currLevel + 1 });
        }
    }
 
    // Return the difference
    return (oddSum - evenSum);
}
 
// Driver Code
int main()
{
    // Create the N-ary Tree
    Node* root = newNode(4);
    root->children.push_back(newNode(2));
    root->children.push_back(newNode(3));
    root->children.push_back(newNode(-5));
    root->children[0]->children.push_back(newNode(-1));
    root->children[0]->children.push_back(newNode(3));
    root->children[2]->children.push_back(newNode(-2));
    root->children[2]->children.push_back(newNode(6));
 
    cout << evenOddLevelDifference(root);
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
 
class GFG{
 
// Structure of a node
// of an n-ary tree
static class Node
{
    int val;
    ArrayList children;
 
    public Node(int val)
    {
        this.val = val;
        this.children = new ArrayList();
    }
};
 
static class Pair
{
    Node first;
    int second;
 
    public Pair(Node node, int val)
    {
        this.first = node;
        this.second = val;
    }
}
 
// Function to find the difference
// between of sums node values of
// odd and even levels in an N-ary tree
static int evenOddLevelDifference(Node root)
{
     
    // Store the sums of nodes at
    // even and odd levels
    int evenSum = 0, oddSum = 0;
 
    // Initialize a queue to store
    // pair of node and level
    Queue q = new LinkedList<>();
 
    // Push the root into the
    // queue with level 1
    q.add(new Pair(root, 1));
 
    // Iterate all levels
    // of tree are traversed
    while (!q.isEmpty())
    {
         
        // Store the node at the
        // front of the queue
        Pair currNode = q.poll();
 
        // Store the current level
        int currLevel = currNode.second;
 
        // Store the current node value
        int currVal = currNode.first.val;
 
        // If current node
        // level is odd
        if (currLevel % 2 == 1)
         
            // Add to odd sum
            oddSum += currVal;
        else
         
            // Add to even sum
            evenSum += currVal;
 
        // Push all the children of current node
        // with increasing current level by 1
        for(Node child : currNode.first.children)
        {
            q.add(new Pair(child, currLevel + 1));
        }
    }
 
    // Return the difference
    return (oddSum - evenSum);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Create the N-ary Tree
    Node root = new Node(4);
    root.children.add(new Node(2));
    root.children.add(new Node(3));
    root.children.add(new Node(-5));
    root.children.get(0).children.add(new Node(-1));
    root.children.get(0).children.add(new Node(3));
    root.children.get(2).children.add(new Node(-2));
    root.children.get(2).children.add(new Node(6));
 
    System.out.println(evenOddLevelDifference(root));
}
}
 
// This code is contributed by sanjeev2552


Python3
# Python3 program to implement
# the above approach
 
# Structure of a node
# of an n-ary tree
class Node:
     
    def __init__(self, val):
         
        self.val = val
        self.children = []
         
# Function to create a
# new tree node
def newNode(val):
 
    temp = Node(val)
     
    return temp
 
# Function to find the difference
# between of sums node values of
# odd and even levels in an N-ary tree
def evenOddLevelDifference(root):
 
    # Store the sums of nodes at
    # even and odd levels
    evenSum = 0
    oddSum = 0
  
    # Initialize a queue to store
    # pair of node and level
    q = []
  
    # Push the root into the
    # queue with level 1
    q.append([root, 1])
  
    # Iterate all levels
    # of tree are traversed
    while (len(q) != 0):
         
        # Store the node at the
        # front of the queue
        currNode = q[0]
  
        # Pop the front node
        q.pop(0)
  
        # Store the current level
        currLevel = currNode[1]
  
        # Store the current node value
        currVal = currNode[0].val
  
        # If current node
        # level is odd
        if (currLevel % 2 != 0):
  
            # Add to odd sum
            oddSum += currVal
        else:
  
            # Add to even sum
            evenSum += currVal
  
        # Push all the children of current node
        # with increasing current level by 1
        for child in currNode[0].children:
            q.append([child, currLevel + 1])
         
    # Return the difference
    return (oddSum - evenSum)
 
# Driver code
if __name__=="__main__":
     
    # Create the N-ary Tree
    root = newNode(4)
    root.children.append(newNode(2))
    root.children.append(newNode(3))
    root.children.append(newNode(-5))
    root.children[0].children.append(newNode(-1))
    root.children[0].children.append(newNode(3))
    root.children[2].children.append(newNode(-2))
    root.children[2].children.append(newNode(6))
  
    print(evenOddLevelDifference(root))
         
# This code is contributed by rutvik_56


C#
// C# program to implement
// the above approach
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
  
// Structure of a node
// of an n-ary tree
class Node
{
    public int val;
    public ArrayList children;
     
    public Node(int val)
    {
        this.val = val;
        this.children = new ArrayList();
    }
};
  
class Pair
{
    public Node first;
    public int second;
     
    public Pair(Node node, int val)
    {
        this.first = node;
        this.second = val;
    }
}
  
// Function to find the difference
// between of sums node values of
// odd and even levels in an N-ary tree
static int evenOddLevelDifference(Node root)
{
     
    // Store the sums of nodes at
    // even and odd levels
    int evenSum = 0, oddSum = 0;
  
    // Initialize a queue to store
    // pair of node and level
    Queue q = new Queue();
  
    // Push the root into the
    // queue with level 1
    q.Enqueue(new Pair(root, 1));
  
    // Iterate all levels
    // of tree are traversed
    while (q.Count != 0)
    {
         
        // Store the node at the
        // front of the queue
        Pair currNode = (Pair)q.Dequeue();
  
        // Store the current level
        int currLevel = currNode.second;
  
        // Store the current node value
        int currVal = currNode.first.val;
  
        // If current node
        // level is odd
        if (currLevel % 2 == 1)
          
            // Add to odd sum
            oddSum += currVal;
        else
          
            // Add to even sum
            evenSum += currVal;
  
        // Push all the children of current node
        // with increasing current level by 1
        foreach(Node child in currNode.first.children)
        {
            q.Enqueue(new Pair(child, currLevel + 1));
        }
    }
  
    // Return the difference
    return(oddSum - evenSum);
}
  
// Driver Code
public static void Main(string[] args)
{
     
    // Create the N-ary Tree
    Node root = new Node(4);
    root.children.Add(new Node(2));
    root.children.Add(new Node(3));
    root.children.Add(new Node(-5));
     
    ((Node)root.children[0]).children.Add(new Node(-1));
    ((Node)root.children[0]).children.Add(new Node(3));
    ((Node)root.children[2]).children.Add(new Node(-2));
    ((Node)root.children[2]).children.Add(new Node(6));
  
    Console.Write(evenOddLevelDifference(root));
}
}
 
// This code is contributed by pratham76


输出:
10

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