The Wayback Machine - https://web.archive.org/web/20240528102546/https://www.geeksforgeeks.org/determine-the-count-of-leaf-nodes-in-an-n-ary-tree/
Open In App

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

Last Updated : 16 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Given the value of ‘N’ and ‘I’. Here, I    represents the number of internal nodes present in an N-ary tree and every node of the N-ary can either have N    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;
}

                    

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

                    

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

                    

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

                    

Javascript

<script>
 
// Javascript program to find number
// of leaf nodes
 
// Function to calculate
// leaf nodes in n-ary tree
function calcNodes(N, I)
{
    var result = 0;
 
    result = I * (N - 1) + 1;
 
    return result;
}
 
// Driver code
var N = 5, I = 2;
 
document.write("Leaf nodes = " + calcNodes(N, I));
 
// This code is contributed by rutvik_56
 
</script>

                    

Output
Leaf nodes = 9

Complexity Analysis:

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)


Previous Article
Next Article

Similar Reads

Count of nodes in a given N-ary tree having distance to all leaf nodes equal in their subtree
Given an N-ary tree root, the task is to find the number of non-leaf nodes in the tree such that all the leaf nodes in the subtree of the current node are at an equal distance from the current node. Example: Input: Tree in the below imageOutput: 4Explanation: The nodes {10, 3, 2, 4} have the distance between them and all the leaf nodes in their sub
11 min read
Remove all leaf nodes from a Generic Tree or N-ary Tree
Given a Generic tree, the task is to delete the leaf nodes from the tree. Examples: Input: 5 / / \ \ 1 2 3 8 / / \ \ 15 4 5 6 Output: 5 : 1 2 3 1 : 2 : 3 : Explanation: Deleted leafs are: 8, 15, 4, 5, 6 Input: 8 / | \ 9 7 2 / | \ | / / | \ \ 4 5 6 10 11 1 2 2 3 Output: 8: 9 7 2 9: 7: 2: Approach: Follow the steps given below to solve the problem Co
9 min read
Number of leaf nodes in the subtree of every node of an n-ary tree
Given an N-ary tree, print the number of leaf nodes in the subtree of every node. Examples: Input: 1 / \ 2 3 / | \ 4 5 6 Output: The node 1 has 4 leaf nodes The node 2 has 1 leaf nodes The node 3 has 3 leaf nodes The node 4 has 1 leaf nodes The node 5 has 1 leaf nodes The node 6 has 1 leaf nodes Approach: The idea is to perform DFS traversal on the
8 min read
Print all leaf nodes of an n-ary tree using DFS
Given an array edge[][2] where (edge[i][0], edge[i][1]) defines an edge in the n-ary tree, the task is to print all the leaf nodes of the given tree using. Examples: Input: edge[][] = {{1, 2}, {1, 3}, {2, 4}, {2, 5}, {3, 6}} Output: 4 5 6 1 / \ 2 3 / \ \ 4 5 6 Input: edge[][] = {{1, 5}, {1, 7}, {5, 6}} Output: 6 7 Approach: DFS can be used to trave
7 min read
Number of leaf nodes in a perfect N-ary tree of height K
Find the number of leaf nodes in a perfect N-ary tree of height K. Note: As the answer can be very large, return the answer modulo 109+7. Examples: Input: N = 2, K = 2Output: 4Explanation: A perfect Binary tree of height 2 has 4 leaf nodes. Input: N = 2, K = 1Output: 2Explanation: A perfect Binary tree of height 1 has 2 leaf nodes. Approach: This p
4 min read
Sum of Bitwise AND of the sum of all leaf and non-leaf nodes for each level of a Binary Tree
Given a Binary Tree consisting of N nodes, the task is to find the sum of Bitwise AND of the sum of all leaf nodes and the sum of all non-leaf nodes for each level in the given Tree. Examples: Input: Below is the given tree: 5 / \ 3 9 / \ 6 4 \ 7Output: 5Explanation: For Level 1: leaf node sum = 0, non-leaf node sum = 5. So, 0&amp;5 = 0. For Level
10 min read
Construct a Tree whose sum of nodes of all the root to leaf path is not divisible by the count of nodes in that path
Given an N-ary tree consisting of N nodes numbered from 1 to N rooted at node 1, the task is to assign values to each node of the tree such that the sum of values from any root to the leaf path which contains at least two nodes is not divisible by the number of nodes along that path. Examples: Input: N = 11, edges[][] = {{1, 2}, {1, 3}, {1, 4}, {1,
11 min read
Count of nodes in given N-ary tree such that their subtree is a Binary Tree
Given an N-ary tree root, the task is to find the count of nodes such that their subtree is a binary tree. Example: Input: Tree in the image below Output: 11Explanation: The nodes such that there subtree is a binary tree are {2, 8, 10, 6, 7, 3, 1, 9, 5, 11, 12}. Input: Tree in the image below Output: 9 Approach: The given problem can be solved by u
11 min read
GCD from root to leaf path in an N-ary tree
Given an N-ary tree and an array val[] which stores the values associated with all the nodes. Also given are a leaf node X and N integers which denotes the value of the node. The task is to find the gcd of all the numbers in the node in the path between leaf to the root. Examples: Input: 1 / \ 2 3 / / \ 4 5 6 / \ 7 8 val[] = { -1, 6, 2, 6, 3, 4, 12
7 min read
Print all root to leaf paths of an N-ary tree
Given an N-Ary tree, the task is to print all root to leaf paths of the given N-ary Tree. Examples: Input: 1 / \ 2 3 / / \ 4 5 6 / \ 7 8 Output:1 2 41 3 51 3 6 71 3 6 8 Input: 1 / | \ 2 5 3 / \ \ 4 5 6Output:1 2 41 2 51 51 3 6 Approach: The idea to solve this problem is to start traversing the N-ary tree using depth-first search and keep inserting
7 min read
Article Tags :
Practice Tags :