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

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++

`// CPP program to find number ` `// of leaf nodes ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate ` `// leaf nodes in n-ary tree ` `int` `calcNodes(` `int` `N, ` `int` `I) ` `{ ` ` ` `int` `result = 0; ` ` ` ` ` `result = I * (N - 1) + 1; ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 5, I = 2; ` ` ` ` ` `cout << ` `"Leaf nodes = "` `<< calcNodes(N, I); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find number ` `// of leaf nodes ` ` ` `class` `GfG ` `{ ` ` ` `// Function to calculate ` `// leaf nodes in n-ary tree ` `static` `int` `calcNodes(` `int` `N, ` `int` `I) ` `{ ` ` ` `int` `result = ` `0` `; ` ` ` ` ` `result = I * (N - ` `1` `) + ` `1` `; ` ` ` ` ` `return` `result; ` `} ` ` ` `// Driver code ` `public` `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 ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find number ` `# of leaf nodes ` ` ` `# Function to calculate ` `# leaf nodes in n-ary tree ` `def` `calcNodes(N, I): ` ` ` `result ` `=` `0` ` ` ` ` `result ` `=` `I ` `*` `(N ` `-` `1` `) ` `+` `1` ` ` ` ` `return` `result ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `5` ` ` `I ` `=` `2` ` ` ` ` `print` `(` `"Leaf nodes = "` `, ` ` ` `calcNodes(N, I)) ` ` ` `# This code is contributed ` `# by SHUBHAMSINGH10 ` |

*chevron_right*

*filter_none*

## C#

// C# program to find number

// of leaf nodes

using System;

class GFG

{

// Function to calculate

// leaf nodes in n-ary tree

static int calcNodes(int N, int I)

{

int result = 0;

result = I * (N – 1) + 1;

return result;

}

// Driver code

public static void Main()

{

int N = 5, I = 2;

Console.Write(“Leaf nodes = ” +

calcNodes(N, I));

}

}

// This code is contributed

// by Akanksha Rai

## PHP

`<?php ` `// PHP program to find number ` `// of leaf nodes ` ` ` `// Function to calculate ` `// leaf nodes in n-ary tree ` `function` `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 ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

Leaf nodes = 9

## Recommended Posts:

- Count Non-Leaf nodes in a Binary Tree
- Program to count leaf nodes in a binary tree
- Iterative program to count leaf nodes in a Binary Tree
- Sum of all leaf nodes of binary tree
- Print all leaf nodes of an n-ary tree using DFS
- Product of all leaf nodes of binary tree
- Maximum sum of leaf nodes among all levels of the given binary tree
- Maximum sum of non-leaf nodes among all levels of the given binary tree
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print all leaf nodes of a Binary Tree from left to right
- Pairwise Swap leaf nodes in a binary tree
- Print all leaf nodes of a binary tree from right to left
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Print the nodes of binary tree as they become the leaf node
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes

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.