# Number of trees whose sum of degrees of all the vertices is L

Given an integer L which is the sum of degrees of all the vertices of some tree. The task is to find the count of all such distinct trees (labeled trees). Two trees are distinct if they have at least a single different edge.

Examples:

Input: L = 2
Output: 1

Input: L = 6
Output: 16

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

Simple Solution: A simple solution is to find the number of nodes of the tree which has sum of degrees of all vertices as L. Number of nodes in such a tree is n = (L / 2 + 1) as described in this article.
Now the solution is to form all the labeled trees which can be formed using n nodes. This approach is quite complex and for larger values of n it is not possible to find out the number of trees using this process.

Efficient Solution: An efficient solution is to find the number of nodes using Cayley’s formula which states that there are n(n – 2) trees with n labeled vertices. So the time complexity of the code now reduces to O(n) which can be further reduced to O(logn) using modular exponentiation.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `#define ll long long int ` ` `  `// Iterative Function to calculate (x^y) in O(log y) ` `ll power(``int` `x, ll y) ` `{ ` ` `  `    ``// Initialize result ` `    ``ll res = 1; ` ` `  `    ``while` `(y > 0) { ` ` `  `        ``// If y is odd, multiply x with result ` `        ``if` `(y & 1) ` `            ``res = (res * x); ` ` `  `        ``// y must be even now ` `        ``// y = y / 2 ` `        ``y = y >> 1; ` `        ``x = (x * x); ` `    ``} ` `    ``return` `res; ` `} ` ` `  `// Function to return the count  ` `// of required trees ` `ll solve(``int` `L) ` `{ ` `    ``// number of nodes ` `    ``int` `n = L / 2 + 1; ` ` `  `    ``ll ans = power(n, n - 2); ` ` `  `    ``// Return the result ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `L = 6; ` ` `  `    ``cout << solve(L); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.io.*; ` ` `  `class` `GFG ` `{ ` `     `  `// Iterative Function to calculate (x^y) in O(log y) ` `static` `long` `power(``int` `x, ``long` `y) ` `{ ` ` `  `    ``// Initialize result ` `    ``long` `res = ``1``; ` ` `  `    ``while` `(y > ``0``) ` `    ``{ ` ` `  `        ``// If y is odd, multiply x with result ` `        ``if` `(y==``1``) ` `            ``res = (res * x); ` ` `  `        ``// y must be even now ` `        ``// y = y / 2 ` `        ``y = y >> ``1``; ` `        ``x = (x * x); ` `    ``} ` `    ``return` `res; ` `} ` ` `  `// Function to return the count  ` `// of required trees ` `static` `long` `solve(``int` `L) ` `{ ` `    ``// number of nodes ` `    ``int` `n = L / ``2` `+ ``1``; ` ` `  `    ``long` `ans = power(n, n - ``2``); ` ` `  `    ``// Return the result ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main (String[] args) ` `{ ` ` `  `    ``int` `L = ``6``; ` `    ``System.out.println (solve(L)); ` `} ` `} ` ` `  `// This code is contributed by ajit.  `

## Python3

 `     `  `# Python implementation of the approach ` ` `  `# Iterative Function to calculate (x^y) in O(log y) ` `def` `power(x, y): ` ` `  `    ``# Initialize result ` `    ``res ``=` `1``; ` ` `  `    ``while` `(y > ``0``): ` ` `  `        ``# If y is odd, multiply x with result ` `        ``if` `(y ``%``2``=``=` `1``): ` `            ``res ``=` `(res ``*` `x); ` ` `  `        ``# y must be even now ` `        ``#y = y / 2 ` `        ``y ``=` `int``(y) >> ``1``; ` `        ``x ``=` `(x ``*` `x); ` `    ``return` `res; ` ` `  ` `  `# Function to return the count  ` `# of required trees ` `def` `solve(L): ` `     `  `    ``# number of nodes ` `    ``n ``=` `L ``/` `2` `+` `1``; ` ` `  `    ``ans ``=` `power(n, n ``-` `2``); ` ` `  `    ``# Return the result ` `    ``return` `int``(ans); ` ` `  `L ``=` `6``; ` `print``(solve(L)); ` ` `  `# This code has been contributed by 29AjayKumar `

## C#

 `// C# implementation of the approach ` `using` `System; ` ` `  `class` `GFG ` `{ ` `     `  `// Iterative Function to calculate (x^y) in O(log y) ` `static` `long` `power(``int` `x, ``long` `y) ` `{ ` ` `  `    ``// Initialize result ` `    ``long` `res = 1; ` ` `  `    ``while` `(y > 0) ` `    ``{ ` ` `  `        ``// If y is odd, multiply x with result ` `        ``if` `(y == 1) ` `            ``res = (res * x); ` ` `  `        ``// y must be even now ` `        ``// y = y / 2 ` `        ``y = y >> 1; ` `        ``x = (x * x); ` `    ``} ` `    ``return` `res; ` `} ` ` `  `// Function to return the count  ` `// of required trees ` `static` `long` `solve(``int` `L) ` `{ ` `    ``// number of nodes ` `    ``int` `n = L / 2 + 1; ` ` `  `    ``long` `ans = power(n, n - 2); ` ` `  `    ``// Return the result ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `static` `public` `void` `Main () ` `{ ` `    ``int` `L = 6; ` `    ``Console.WriteLine(solve(L)); ` `} ` `} ` ` `  `// This code is contributed by Tushil.  `

Output:

```16
```

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.

Improved By : jit_t, 29AjayKumar

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.
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0}