Number of leaf nodes in the subtree of every node of an n-ary tree
Last Updated :
09 Sep, 2022
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 given tree and for every node keep an array leaf[] to store the count of leaf nodes in the subtree below it.
Now, while recurring down the tree, if a leaf node is found set its leaf[i] value to 1 and return back in upward direction. Now, every time while returning back from the function call to upward, add the leaf nodes of the node below it.
Once, the DFS traversal is completed we will have the count of leaf nodes in the array leaf[].
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void insert( int x, int y, vector< int > adjacency[])
{
adjacency[x].push_back(y);
}
void dfs( int node, int leaf[], int vis[],
vector< int > adjacency[])
{
leaf[node] = 0;
vis[node] = 1;
for ( auto it : adjacency[node]) {
if (!vis[it]) {
dfs(it, leaf, vis, adjacency);
leaf[node] += leaf[it];
}
}
if (!adjacency[node].size())
leaf[node] = 1;
}
void printLeaf( int n, int leaf[])
{
for ( int i = 1; i <= n; i++) {
cout << "The node " << i << " has "
<< leaf[i] << " leaf nodes\n" ;
}
}
int main()
{
int N = 6;
vector< int > adjacency[N + 1];
insert(1, 2, adjacency);
insert(1, 3, adjacency);
insert(3, 4, adjacency);
insert(3, 5, adjacency);
insert(3, 6, adjacency);
int leaf[N + 1];
int vis[N + 1] = { 0 };
dfs(1, leaf, vis, adjacency);
printLeaf(N, leaf);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static Vector<Vector<Integer>> adjacency = new
Vector<Vector<Integer>>();
static void insert( int x, int y)
{
adjacency.get(x).add(y);
}
static void dfs( int node, int leaf[], int vis[])
{
leaf[node] = 0 ;
vis[node] = 1 ;
for ( int i = 0 ; i < adjacency.get(node).size(); i++)
{
int it = adjacency.get(node).get(i);
if (vis[it] == 0 )
{
dfs(it, leaf, vis);
leaf[node] += leaf[it];
}
}
if (adjacency.get(node).size() == 0 )
leaf[node] = 1 ;
}
static void printLeaf( int n, int leaf[])
{
for ( int i = 1 ; i <= n; i++)
{
System.out.print( "The node " + i + " has " +
leaf[i] + " leaf nodes\n" );
}
}
public static void main(String args[])
{
int N = 6 ;
for ( int i = 0 ; i <= N; i++)
adjacency.add( new Vector<Integer>());
insert( 1 , 2 );
insert( 1 , 3 );
insert( 3 , 4 );
insert( 3 , 5 );
insert( 3 , 6 );
int leaf[] = new int [N + 1 ];
int vis[] = new int [N + 1 ] ;
dfs( 1 , leaf, vis);
printLeaf(N, leaf);
}
}
|
Python3
adjacency = [[] for i in range ( 100 )]
def insert(x, y):
adjacency[x].append(y)
def dfs(node, leaf, vis):
leaf[node] = 0
vis[node] = 1
for it in adjacency[node]:
if (vis[it] = = False ):
dfs(it, leaf, vis)
leaf[node] + = leaf[it]
if ( len (adjacency[node]) = = 0 ):
leaf[node] = 1
def printLeaf(n, leaf):
for i in range ( 1 , n + 1 ):
print ( "The node" , i, "has" ,
leaf[i], "leaf nodes" )
N = 6
insert( 1 , 2 )
insert( 1 , 3 )
insert( 3 , 4 )
insert( 3 , 5 )
insert( 3 , 6 )
leaf = [ 0 for i in range (N + 1 )]
vis = [ 0 for i in range (N + 1 )]
dfs( 1 , leaf, vis)
printLeaf(N, leaf)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static List<List< int >> adjacency = new
List<List< int >>();
static void insert( int x, int y)
{
adjacency[x].Add(y);
}
static void dfs( int node, int []leaf, int []vis)
{
leaf[node] = 0;
vis[node] = 1;
for ( int i = 0; i < adjacency[node].Count; i++)
{
int it = adjacency[node][i];
if (vis[it] == 0)
{
dfs(it, leaf, vis);
leaf[node] += leaf[it];
}
}
if (adjacency[node].Count == 0)
leaf[node] = 1;
}
static void printLeaf( int n, int []leaf)
{
for ( int i = 1; i <= n; i++)
{
Console.Write( "The node " + i + " has " +
leaf[i] + " leaf nodes\n" );
}
}
public static void Main(String []args)
{
int N = 6;
for ( int i = 0; i <= N; i++)
adjacency.Add( new List< int >());
insert(1, 2);
insert(1, 3);
insert(3, 4);
insert(3, 5);
insert(3, 6);
int []leaf = new int [N + 1];
int []vis = new int [N + 1] ;
dfs(1, leaf, vis);
printLeaf(N, leaf);
}
}
|
Javascript
<script>
let adjacency = [];
function insert(x,y)
{
adjacency[x].push(y);
}
function dfs(node,leaf,vis)
{
leaf[node] = 0;
vis[node] = 1;
for (let i = 0; i < adjacency[node].length; i++)
{
let it = adjacency[node][i];
if (vis[it] == 0)
{
dfs(it, leaf, vis);
leaf[node] += leaf[it];
}
}
if (adjacency[node].length == 0)
leaf[node] = 1;
}
function printLeaf(n,leaf)
{
for (let i = 1; i <= n; i++)
{
document.write( "The node " + i + " has " +
leaf[i] + " leaf nodes<br>" );
}
}
let N = 6;
for (let i = 0; i <= N; i++)
adjacency.push([]);
insert(1, 2);
insert(1, 3);
insert(3, 4);
insert(3, 5);
insert(3, 6);
let leaf = new Array(N + 1);
for (let i=0;i<leaf.length;i++)
{
leaf[i]=0;
}
let vis = new Array(N + 1) ;
for (let i=0;i<vis.length;i++)
{
vis[i]=0;
}
dfs(1, leaf, vis);
printLeaf(N, leaf);
</script>
|
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
Complexity Analysis:
- Time Complexity: O(N), as we are traversing the tree using DFS(recursion), where N is the number of nodes in the tree.
- Auxiliary Space: O(N), as we are using extra space for the tree. Where N is the number of nodes in the tree.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...