Given a Prufer sequence of a Tree, the task is to print the node with the maximum degree in the tree whose Prufer sequence is given. In case there are many nodes with maximum degree, print the node with the smallest number.
Examples:
Input: a[] = {4, 1, 3, 4}
Output: 4
The tree is:
2----4----3----1----5
|
6
Input: a[] = {1, 2, 2}
Output: 2
A simple approach is to create the tree using the Prufer sequence and then find the degree of all the nodes and then find the maximum among them.
Efficient approach: Create a degree[] array of size 2 more than the length of the Prufer sequence, since the length of prufer sequence is N – 2 if N is the number of nodes. Initially, fill the degree array with 1. Iterate in the Prufer sequence and increase the frequency in the degree table for every element. This method works because the frequency of a node in the Prufer sequence is one less than the degree in the tree. Now iterate in the degree array and find the node with the maximum frequency which will be our answer.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std;// Function to return the node with// the maximum degree in the tree// whose Prufer sequence is givenint 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 codeint main(){ int a[] = { 1, 2, 2 }; int n = sizeof(a) / sizeof(a[0]); cout << findMaxDegreeNode(a, n); return 0;} |
Java
// Java implementation of the approachimport 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 |
Python3
# Python implementation of the approach# Function to return the node with# the maximum degree in the tree# whose Prufer sequence is givendef 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 codea = [ 1, 2, 2 ];n = len(a);print(findMaxDegreeNode(a, n));# This code has been contributed by 29AjayKumar |
C#
// C# implementation of the approachusing 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 |
Javascript
<script> // Javascript implementation of the approach // Function to return the node with // the maximum degree in the tree // whose Prufer sequence is given function findMaxDegreeNode(prufer, n) { let nodes = n + 2; // Hash-table to mark the // degree of every node let degree = new Array(n + 2 + 1); degree.fill(0); // Initially let all the degrees be 1 for (let i = 1; i <= nodes; i++) degree[i] = 1; // Increase the count of the degree for (let i = 0; i < n; i++) degree[prufer[i]]++; let maxDegree = 0; let node = 0; // Find the node with maximum degree for (let i = 1; i <= nodes; i++) { if (degree[i] > maxDegree) { maxDegree = degree[i]; node = i; } } return node; } let a = [ 1, 2, 2 ]; let n = a.length; document.write(findMaxDegreeNode(a, n));</script> |
2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
In case you wish to attend live classes with industry experts, please refer Geeks Classes Live and Geeks Classes Live USA


