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

The number of trees with a given whole number of degrees, L, can be decided by an equation based on the chart hypothesis. To begin with, we note that the whole number of degrees in a tree with N vertices is continuously 2N-2. Utilising this, we are able to calculate the number of clears out within the tree, which is L minus 2. Another way is to determine the number of inner vertices by subtracting the number of takeoffs from the overall number of vertices. At long last, we were able to discover the number of ways to disseminate the remaining degrees among the inside vertices by employing a combination equation. Hence, the number of trees with a whole of degrees L can be computed utilising these steps.

Methods Used

  • Recursive Enumeration

  • Combinatorial analysis

Recursive Enumeration

Recursive Count could be a strategy for deciding the number of trees with a given number of degrees, L. Beginning with a single vertex, we systematically include modern vertices and edges to make trees. At each step, we convey the remaining degrees among the existing vertices while keeping up the specified sum. This handle is rehashed recursively, investigating all conceivable combinations. By backtracking and considering different degrees, we are able to determine the number of substantial trees. This approach is valuable for smaller input sizes but may end up being wasteful for larger values of L due to its exponential time complexity.


  • Characterise a recursive work called countTrees(L, vertexCount), where L is the required whole number of degrees and vertexCount represents the number of vertices within the tree.

  • Base case.

  • If L breaks even with 2 and vertexCount equals 1, return 1 (since a single vertex could be a substantial tree). Initialise the variable treeCount to 0.

  • Iterate over conceivable degrees for the current vertex, beginning from 1 up to L-1.

  • Inside the loop.

  • Subtract the current degree from L and recursively call countTrees with the upgraded L and vertexCount-1. the returned value to treeCount. Reurn treeCount.

  • Call the count. Trees work with the required L and the beginning vertex count (as per Rule 1) to get the number of trees with the given entirety of degrees.


#include <iostream>

int countTrees(int L, int vertexCount) {
   if (L == 2 && vertexCount == 1) {
     return 1;

   int treeCount = 0;
   for (int degree = 1; degree <= L - 1; degree++) {
      int remainingSum = L - degree;
      int subTreeCount = countTrees(remainingSum, vertexCount - 1);
      treeCount += subTreeCount;

   return treeCount;

int main() {
   int L = 8;
   int vertexCount = 4;

   int numTrees = countTrees(L, vertexCount);
   std::cout << "Number of trees with sum of degrees " << L << " and " << vertexCount << " vertices: " << numTrees << std::endl;

   return 0;


Number of trees with sum of degrees 8 and 4 vertices: 10

Combinatorial Analysis

Combinatorial analysis, within the context of deciding the number of trees with a given whole of degrees L, includes considering the structure of trees and utilising combinatorial strategies to number them. It involves analysing the imperatives forced by the entirety of degrees, distinguishing the number of inside vertices and clearing them out, and disseminating the remaining degrees among them. By utilising concepts such as combinations, stages, and repeat relations, combinatorial examination permits the advancement of equations or calculations to specifically compute the number of trees for a particular entirety of degrees L. This approach leverages scientific standards to unravel the issue efficiently and proficiently.


  • Initialise the variable count_trees to 0.

  • Iterate over the conceivable number of clears out, i, from 1 to L-2.

  • Calculate the number of inside vertices, internal vertices, as L - i.

  • Use combinatorial methods to convey the remaining degrees among the inside vertices. You'll be able to either utilise a recursive approach or energetic programming to calculate this distribution.

  • Increment count_trees by the item of the number of ways to convey the degrees among inside vertices and the number of ways to select the leaves.

  • Return count_trees as the result.


#include <iostream>
#include <algorithm>
#include <vector>

int countTrees(int L) {
   int count_trees = 0;
   for (int i = 1; i <= L - 2; i++) {
      int internal_vertices = L - i;
      // Calculate the number of ways to distribute degrees among internal vertices
      std::vector<int> degrees(internal_vertices - 2, 1);
      do {
         // Calculate the number of ways to select the leaves
         int leaf_selection = std::count_if(degrees.begin(), degrees.end(), [](int x) {
            return x == 1;
         count_trees += leaf_selection;
      } while (std::next_permutation(degrees.begin(), degrees.end()));

   return count_trees;

int main() {
   int L = 10; // Assuming L = 10
   int result = countTrees(L);

   std::cout << "Number of trees: " << result << std::endl;

   return 0;


Number of trees: 168


This article examines two strategies for deciding the number of trees with a particular number of degrees in a chart. The primary strategy, recursive identification, includes efficiently adding vertices and edges to make trees while conveying the remaining degrees. The moment strategy, combinatorial investigation, utilises numerical standards and combinatorial strategies to calculate the number of trees by conveying the degrees among the vertices. Both approaches provide efficient ways to illuminate the issue and are pertinent in completely different scenarios.

Updated on: 19-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started