原文:

给定一个树的 ,任务是打印给定 prufer 序列的树中具有最大度的节点。如果最大度数的节点很多,打印最小度数的节点。 例:

input: a[] = {4, 1, 3, 4} 
output: 4
the tree is:
2----4----3----1----5
     |
     6 
input: a[] = {1, 2, 2} 
output: 2

一种简单的方法是使用普鲁夫序列创建树,然后找到所有节点的度,然后找到其中的最大值。 高效方法:创建一个大小为 2 的度【】数组,大于普鲁弗序列的长度,因为如果 n 是节点数,普鲁弗序列的长度是n–2。首先,用 1 填充度数数组。迭代普鲁弗序列,增加每个元素在度表中的出现频率。这种方法之所以有效,是因为普鲁弗序列中一个节点的频率比树中的度数少一个。现在迭代度数组,找到最大频率的节点,这就是我们的答案。 以下是上述方法的实施:

c

// c   implementation of the approach
#include 
using namespace std;
// function to return the node with
// the maximum degree in the tree
// whose prufer sequence is given
int findmaxdegreenode(int prufer[], int n)
{
    int nodes = n   2;
    // hash-table to mark the
    // degree of every node
    int degree[n   2   1];
    // initially let all the degrees be 1
    for (int i = 1; i <= nodes; i  )
        degree[i] = 1;
    // increase the count of the degree
    for (int i = 0; i < n; i  )
        degree[prufer[i]]  ;
    int maxdegree = 0;
    int node = 0;
    // find the node with maximum degree
    for (int i = 1; i <= nodes; i  ) {
        if (degree[i] > maxdegree) {
            maxdegree = degree[i];
            node = i;
        }
    }
    return node;
}
// driver code
int main()
{
    int a[] = { 1, 2, 2 };
    int n = sizeof(a) / sizeof(a[0]);
    cout << findmaxdegreenode(a, n);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java implementation of the approach
import java.io.*;
class gfg
{
    // function to return the node with
    // the maximum degree in the tree
    // whose prufer sequence is given
    static int findmaxdegreenode(int prufer[], int n)
    {
        int nodes = n   2;
        // hash-table to mark the
        // degree of every node
        int []degree = new int[n   2   1];
        // initially let all the degrees be 1
        for (int i = 1; i <= nodes; i  )
            degree[i] = 1;
        // increase the count of the degree
        for (int i = 0; i < n; i  )
            degree[prufer[i]]  ;
        int maxdegree = 0;
        int node = 0;
        // find the node with maximum degree
        for (int i = 1; i <= nodes; i  )
        {
            if (degree[i] > maxdegree)
            {
                maxdegree = degree[i];
                node = i;
            }
        }
        return node;
    }
    // driver code
    public static void main (string[] args)
    {
        int []a = { 1, 2, 2 };
        int n = a.length;
        system.out.println(findmaxdegreenode(a, n));
    }
}
// this code is contributed by ajit_00023

python 3

# python implementation of the approach
# function to return the node with
# the maximum degree in the tree
# whose prufer sequence is given
def findmaxdegreenode(prufer, n):
    nodes = n   2;
    # hash-table to mark the
    # degree of every node
    degree = [0]*(n   2   1);
    # initially let all the degrees be 1
    for i in range(1,nodes 1):
        degree[i] = 1;
    # increase the count of the degree
    for i in range(n):
        degree[prufer[i]] =1;
    maxdegree = 0;
    node = 0;
    # find the node with maximum degree
    for i in range(1,nodes 1):
        if (degree[i] > maxdegree):
            maxdegree = degree[i];
            node = i;
    return node;
# driver code
a = [ 1, 2, 2 ];
n = len(a);
print(findmaxdegreenode(a, n));
# this code has been contributed by 29ajaykumar

c

// c# implementation of the approach
using system;
class gfg
{
    // function to return the node with
    // the maximum degree in the tree
    // whose prufer sequence is given
    static int findmaxdegreenode(int []prufer, int n)
    {
        int nodes = n   2;
        // hash-table to mark the
        // degree of every node
        int []degree = new int[n   2   1];
        // initially let all the degrees be 1
        for (int i = 1; i <= nodes; i  )
            degree[i] = 1;
        // increase the count of the degree
        for (int i = 0; i < n; i  )
            degree[prufer[i]]  ;
        int maxdegree = 0;
        int node = 0;
        // find the node with maximum degree
        for (int i = 1; i <= nodes; i  )
        {
            if (degree[i] > maxdegree)
            {
                maxdegree = degree[i];
                node = i;
            }
        }
        return node;
    }
    // driver code
    static public void main ()
    {
        int []a = { 1, 2, 2 };
        int n = a.length;
        console.writeline(findmaxdegreenode(a, n));
    }
}
// this code is contributed by ankitrai01

java 描述语言


output: 

2