# Print the degree of every node from the given Prufer sequence

Given a Prufer sequence, the task is to find the degrees of all the nodes of the tree made by the prufer sequence.

Examples:

```Input: arr[] = {4, 1, 3, 4}
Output: 2 1 2 3 1 1

The tree is:
2----4----3----1----5
|
6

Input: arr[] = {1, 2, 2}
Output: 2 3 1 1 1
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

A simple approach is to create the tree using the Prufer sequence and then find the degree of all the nodes.

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.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to print the degrees of every ` `// node in the tree made by ` `// the given Prufer sequence ` `void` `printDegree(``int` `prufer[], ``int` `n) ` `{ ` `    ``int` `node = 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 <= node; i++) ` `        ``degree[i] = 1; ` ` `  `    ``// Increase the count of the degree ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``degree[prufer[i]]++; ` ` `  `    ``// Print the degree of every node ` `    ``for` `(``int` `i = 1; i <= node; i++) { ` `        ``cout << degree[i] << ``" "``; ` `    ``} ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `a[] = { 4, 1, 3, 4 }; ` `    ``int` `n = ``sizeof``(a) / ``sizeof``(a[0]); ` `    ``printDegree(a, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG  ` `{ ` ` `  `    ``// Function to print the degrees of every ` `    ``// node in the tree made by ` `    ``// the given Prufer sequence ` `    ``static` `void` `printDegree(``int` `prufer[], ``int` `n)  ` `    ``{ ` `        ``int` `node = 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 <= node; i++) ` `        ``{ ` `            ``degree[i] = ``1``; ` `        ``} ` ` `  `        ``// Increase the count of the degree ` `        ``for` `(``int` `i = ``0``; i < n; i++)  ` `        ``{ ` `            ``degree[prufer[i]]++; ` `        ``} ` ` `  `        ``// Print the degree of every node ` `        ``for` `(``int` `i = ``1``; i <= node; i++)  ` `        ``{ ` `            ``System.out.print(degree[i] + ``" "``); ` `        ``} ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String[] args)  ` `    ``{ ` `        ``int` `a[] = {``4``, ``1``, ``3``, ``4``}; ` `        ``int` `n = a.length; ` `        ``printDegree(a, n); ` `    ``} ` `} ` ` `  `/* This code contributed by PrinciRaj1992 */`

## Python3

 `# Python3 implementation of the approach  ` ` `  `# Function to print the degrees of  ` `# every node in the tree made by  ` `# the given Prufer sequence  ` `def` `printDegree(prufer, n):  ` `  `  `    ``node ``=` `n ``+` `2`  ` `  `    ``# Hash-table to mark the  ` `    ``# degree of every node  ` `    ``degree ``=` `[``1``] ``*` `(n ``+` `2` `+` `1``)  ` ` `  `    ``# Increase the count of the degree  ` `    ``for` `i ``in` `range``(``0``, n):  ` `        ``degree[prufer[i]] ``+``=` `1`  ` `  `    ``# Print the degree of every node  ` `    ``for` `i ``in` `range``(``1``, node``+``1``):   ` `        ``print``(degree[i], end ``=` `" "``)  ` `      `  `# Driver code  ` `if` `__name__ ``=``=` `"__main__"``: ` `  `  `    ``a ``=` `[``4``, ``1``, ``3``, ``4``]  ` `    ``n ``=` `len``(a)  ` `    ``printDegree(a, n)  ` ` `  `# This code is contributed by Rituraj Jain `

## C#

 `// C# implementation of the approach ` `using` `System; ` `     `  `class` `GFG  ` `{ ` ` `  `// Function to print the degrees of every ` `// node in the tree made by ` `// the given Prufer sequence ` `static` `void` `printDegree(``int` `[]prufer, ``int` `n)  ` `{ ` `    ``int` `node = 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 <= node; i++) ` `    ``{ ` `        ``degree[i] = 1; ` `    ``} ` ` `  `    ``// Increase the count of the degree ` `    ``for` `(``int` `i = 0; i < n; i++)  ` `    ``{ ` `        ``degree[prufer[i]]++; ` `    ``} ` ` `  `    ``// Print the degree of every node ` `    ``for` `(``int` `i = 1; i <= node; i++)  ` `    ``{ ` `        ``Console.Write(degree[i] + ``" "``); ` `    ``} ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args)  ` `{ ` `    ``int` `[]a = {4, 1, 3, 4}; ` `    ``int` `n = a.Length; ` `    ``printDegree(a, n); ` `} ` `} ` ` `  `// This code is contributed by 29AjayKumar `

Output:

```2 1 2 3 1 1
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.