Determine the count of Leaf nodes in an N-ary tree - GeeksforGeeks

13 Apr, 2021
Given the value of ‘N’ and ‘I’. Here, represents the number of internal nodes present in an N-ary tree and every node of the N-ary can either have childs or zero child. The task is to determine the number of Leaf nodes in n-ary tree.
Examples:  
Input : N = 3, I = 5 Output : Leaf nodes = 11  
Input : N = 10, I = 10 Output : leaf nodes = 91  
Formula: 
where, I = Number of Internal nodes. L = Leaf Nodes. and, N = Number of children each node can have.  
Derivation: The tree is an N-ary tree. Assume it has T total nodes, which is the sum of internal nodes (I) and leaf nodes (L). A tree with T total nodes will have (T – 1) edges or branches.In other words, since the tree is an N-ary tree, each internal node will have N branches contributing a total of N*I internal branches. Therefore we have the following relations from the above explanations,
N * I = T – 1
L + I = T
From the above two equations, we can say that L = (N – 1) * I + 1.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
PHP
Javascript
// CPP program to find number// of leaf nodes #include <bits/stdc++.h>using namespace std; // Function to calculate// leaf nodes in n-ary treeint calcNodes(int N, int I){    int result = 0;     result = I * (N - 1) + 1;     return result;} // Driver codeint main(){    int N = 5, I = 2;     cout << "Leaf nodes = " << calcNodes(N, I);     return 0;}
// Java program to find number// of leaf nodes class GfG{ // Function to calculate// leaf nodes in n-ary treestatic int calcNodes(int N, int I){    int result = 0;     result = I * (N - 1) + 1;     return result;} // Driver codepublic static void main(String[] args){    int N = 5, I = 2;     System.out.println("Leaf nodes = " +                        calcNodes(N, I));}} // This code is contributed by Prerna Saini
# Python3 program to find number# of leaf nodes # Function to calculate# leaf nodes in n-ary treedef calcNodes(N, I):    result = 0     result = I * (N - 1) + 1     return result # Driver Codeif __name__ == '__main__':    N = 5    I = 2     print("Leaf nodes = ",           calcNodes(N, I)) # This code is contributed# by SHUBHAMSINGH10
// C# program to find number// of leaf nodesusing System; class GFG{ // Function to calculate// leaf nodes in n-ary treestatic int calcNodes(int N, int I){    int result = 0;     result = I * (N - 1) + 1;     return result;} // Driver codepublic static void Main(){    int N = 5, I = 2;     Console.Write("Leaf nodes = " +                  calcNodes(N, I));}} // This code is contributed// by Akanksha Rai
<?php// PHP program to find number// of leaf nodes // Function to calculate// leaf nodes in n-ary treefunction calcNodes($N, $I){    $result = 0;     $result = $I * ($N - 1) + 1;     return $result;} // Driver code$N = 5; $I = 2; echo "Leaf nodes = " .      calcNodes($N, $I); // This code is contributed// by Akanksha Rai?>
<script> // Javascript program to find number// of leaf nodes // Function to calculate// leaf nodes in n-ary treefunction calcNodes(N, I){    var result = 0;     result = I * (N - 1) + 1;     return result;} // Driver codevar N = 5, I = 2; document.write("Leaf nodes = " + calcNodes(N, I)); // This code is contributed by rutvik_56 </script>
Leaf nodes = 9
 
prerna saini
SHUBHAMSINGH10
Akanksha_Rai
rutvik_56
n-ary-tree
C++ Programs
Mathematical
Tree
Mathematical
Tree
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Passing a function as a parameter in C++
Const keyword in C++
Program to implement Singly Linked List in C++ using class
cout in C++
Dynamic _Cast in C++
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7