📌  相关文章
📜  在给定的树中找到最大的偶数和节点

📅  最后修改于: 2021-09-05 11:35:20             🧑  作者: Mango

给定一个包含所有节点权重的,任务是找到权重为偶数和的最大权重节点。

例子:

Input: Tree =
                 5
               /  \
             10    6
            /  \ 
           11   8  
Output: 11
Explanation:
The tree node weights are:
5 -> 5
10 -> 1 + 0 = 1
6 -> 6
11 -> 1 + 1 = 2
8 -> 8
Here, digit sum for nodes
containing 11, 6 and 8 are even.
Hence, the maximum weighing
even digit sum node is 11.

Input: Tree =
                1
               /  \
              4    7
             / \   / \
            11  3 15  8
Output: 15
Explanation:
Here, digit sum for nodes containing
4, 11, 15 and 8 are even. 
Hence, the maximum weighing 
even digit sum node is 15.

方法:解决上述问题,请按照以下步骤操作:

  • 这个想法是对树和每个节点执行深度优先搜索。
  • 首先,通过迭代每个数字找到节点中存在的权重的数字和,然后检查节点是否具有偶数数字和。
  • 最后,如果它有偶数和,则检查该节点是否是我们迄今为止发现的最大偶数和节点,如果是,则将该节点设为最大偶数和节点。

下面是上述方法的实现:

C++
// C++ program to find the
// maximum weighing node
// in the tree whose weight
// has an Even Digit Sum
 
#include 
using namespace std;
 
const int sz = 1e5;
int ans = -1;
 
vector graph[100];
vector weight(100);
 
// Function to find the digit sum
// for a number
int digitSum(int num)
{
    int sum = 0;
    while (num)
    {
        sum += (num % 10);
        num /= 10;
    }
 
    return sum;
}
 
// Function to perform dfs
void dfs(int node, int parent)
{
    // Check if the digit sum
    // of the node weight
    // is even or not
    if (!(digitSum(weight[node]) & 1))
        ans = max(ans, weight[node]);
 
    // Performing DFS to iterate the
    // remaining nodes
    for (int to : graph[node]) {
        if (to == parent)
            continue;
        dfs(to, node);
    }
}
 
// Driver code
int main()
{
    // Weights of the node
    weight[1] = 5;
    weight[2] = 10;
    weight[3] = 11;
    weight[4] = 8;
    weight[5] = 6;
 
    // Edges of the tree
    graph[1].push_back(2);
    graph[2].push_back(3);
    graph[2].push_back(4);
    graph[1].push_back(5);
 
    // Call the dfs function to
    // traverse through the tree
    dfs(1, 1);
 
    cout << ans << endl;
 
    return 0;
}


Java
// Java program to find the
// maximum weighing node
// in the tree whose weight
// has an Even Digit Sum
import java.util.*;
class GFG
{
 
    static int sz = (int)1e5;
    static int ans = -1;
 
    static Vector[] graph = new Vector[100];
    static int[] weight = new int[100];
 
    // Function to find the digit sum
    // for a number
    static int digitSum(int num)
    {
        int sum = 0;
        while (num > 0)
        {
            sum += (num % 10);
            num /= 10;
        }
        return sum;
    }
 
    // Function to perform dfs
    static void dfs(int node, int parent)
    {
        // Check if the digit sum
        // of the node weight
        // is even or not
        if (!(digitSum(weight[node]) % 2 == 1))
            ans = Math.max(ans, weight[node]);
 
        // Performing DFS to iterate the
        // remaining nodes
        for (int to : graph[node])
        {
            if (to == parent)
                continue;
            dfs(to, node);
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        // Weights of the node
        weight[1] = 5;
        weight[2] = 10;
        weight[3] = 11;
        weight[4] = 8;
        weight[5] = 6;
        for (int i = 0; i < graph.length; i++)
            graph[i] = new Vector();
        // Edges of the tree
        graph[1].add(2);
        graph[2].add(3);
        graph[2].add(4);
        graph[1].add(5);
 
        // Call the dfs function to
        // traverse through the tree
        dfs(1, 1);
 
        System.out.print(ans + "\n");
    }
}
 
// This code contributed by Princi Singh


Python3
# Python3 program to find the
# maximum weighing node
# in the tree whose weight
# has an Even Digit Sum
sz = 1e5
ans = -1
 
graph = [[] for i in range(100)]
weight = [-1] * 100
 
# Function to find the digit sum
# for a number
 
 
def digitSum(num):
    sum = 0
 
    while num:
        sum += num % 10
        num = num // 10
 
    return sum
 
# Function to perform dfs
 
 
def dfs(node, parent):
    global ans
 
    # Check if the digit sum
    # of the node weight
    # is even or not
    if not(digitSum(weight[node]) & 1):
        ans = max(ans, weight[node])
 
    # Performing DFS to iterate the
    # remaining nodes
    for to in graph[node]:
        if to == parent:
            continue
 
        dfs(to, node)
 
# Driver code
 
 
def main():
 
    # Weights of the node
    weight[1] = 5
    weight[2] = 10
    weight[3] = 11
    weight[4] = 8
    weight[5] = 6
 
    # Edges of the tree
    graph[1].append(2)
    graph[2].append(3)
    graph[2].append(4)
    graph[1].append(5)
 
    # Call the dfs function to
    # traverse through the tree
    dfs(1, 1)
 
    print(ans)
 
 
main()
 
# This code is contributed by stutipathak31jan


C#
// C# program to find the
// maximum weighing node
// in the tree whose weight
// has an Even Digit Sum
using System;
using System.Collections.Generic;
class GFG
{
 
    static int sz = (int)1e5;
    static int ans = -1;
 
    static List[] graph = new List[ 100 ];
    static int[] weight = new int[100];
 
    // Function to find the digit sum
    // for a number
    static int digitSum(int num)
    {
        int sum = 0;
        while (num > 0)
        {
            sum += (num % 10);
            num /= 10;
        }
        return sum;
    }
 
    // Function to perform dfs
    static void dfs(int node, int parent)
    {
        // Check if the digit sum
        // of the node weight
        // is even or not
        if (!(digitSum(weight[node]) % 2 == 1))
            ans = Math.Max(ans, weight[node]);
 
        // Performing DFS to iterate the
        // remaining nodes
        foreach(int to in graph[node])
        {
            if (to == parent)
                continue;
            dfs(to, node);
        }
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        // Weights of the node
        weight[1] = 5;
        weight[2] = 10;
        weight[3] = 11;
        weight[4] = 8;
        weight[5] = 6;
        for (int i = 0; i < graph.Length; i++)
            graph[i] = new List();
 
        // Edges of the tree
        graph[1].Add(2);
        graph[2].Add(3);
        graph[2].Add(4);
        graph[1].Add(5);
 
        // Call the dfs function to
        // traverse through the tree
        dfs(1, 1);
 
        Console.Write(ans + "\n");
    }
}
 
// This code is contributed by Rajput-Ji


输出

11

复杂度分析:

时间复杂度: O(N)。
在 dfs 中,树的每个节点都被处理一次,因此如果树中总共有 N 个节点,则由于 dfs 的复杂性是 O(N)。此外,为了处理每个节点,使用 DigitSum()函数,其复杂度为 O(d),其中 d 是节点权重的位数,但是由于任何节点的权重都是整数,因此它可以最多只有 10 位数字,因此此函数不会影响整体时间复杂度。因此,时间复杂度为 O(N)。

辅助空间: O(1)。
不需要任何额外的空间,因此空间复杂度是恒定的。

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