# 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 6Output: 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 it’s 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++

`// C++ program to print the number of ` `// leaf nodes of every node ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to insert edges of tree ` `void` `insert(` `int` `x, ` `int` `y, vector<` `int` `> adjacency[]) ` `{ ` ` ` `adjacency[x].push_back(y); ` `} ` ` ` `// Function to run DFS on a tree ` `void` `dfs(` `int` `node, ` `int` `leaf[], ` `int` `vis[], ` ` ` `vector<` `int` `> adjacency[]) ` `{ ` ` ` `leaf[node] = 0; ` ` ` `vis[node] = 1; ` ` ` ` ` `// iterate on all the nodes ` ` ` `// connected to node ` ` ` `for` `(` `auto` `it : adjacency[node]) { ` ` ` ` ` `// If not visited ` ` ` `if` `(!vis[it]) { ` ` ` `dfs(it, leaf, vis, adjacency); ` ` ` `leaf[node] += leaf[it]; ` ` ` `} ` ` ` `} ` ` ` ` ` `if` `(!adjacency[node].size()) ` ` ` `leaf[node] = 1; ` `} ` ` ` `// Function to print number of ` `// leaf nodes of a node ` `void` `printLeaf(` `int` `n, ` `int` `leaf[]) ` `{ ` ` ` `// Function to print leaf nodes ` ` ` `for` `(` `int` `i = 1; i <= n; i++) { ` ` ` `cout << ` `"The node "` `<< i << ` `" has "` ` ` `<< leaf[i] << ` `" leaf nodes\n"` `; ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// Given N-ary Tree ` ` ` ` ` `/* 1 ` ` ` `/ \ ` ` ` `2 3 ` ` ` `/ | \ ` ` ` `4 5 6 */` ` ` ` ` `int` `N = 6; ` `// no of nodes ` ` ` `vector<` `int` `> adjacency[N + 1]; ` `// adjacency list for tree ` ` ` ` ` `insert(1, 2, adjacency); ` ` ` `insert(1, 3, adjacency); ` ` ` `insert(3, 4, adjacency); ` ` ` `insert(3, 5, adjacency); ` ` ` `insert(3, 6, adjacency); ` ` ` ` ` `int` `leaf[N + 1]; ` `// Store count of leaf in subtree of i ` ` ` `int` `vis[N + 1] = { 0 }; ` `// mark nodes visited ` ` ` ` ` `dfs(1, leaf, vis, adjacency); ` ` ` ` ` `printLeaf(N, leaf); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to prthe number of ` `# leaf nodes of every node ` `adjacency ` `=` `[[] ` `for` `i ` `in` `range` `(` `100` `)] ` ` ` `# Function to insert edges of tree ` `def` `insert(x, y): ` ` ` `adjacency[x].append(y) ` ` ` `# Function to run DFS on a tree ` `def` `dfs(node, leaf, vis): ` ` ` ` ` `leaf[node] ` `=` `0` ` ` `vis[node] ` `=` `1` ` ` ` ` `# iterate on all the nodes ` ` ` `# connected to node ` ` ` `for` `it ` `in` `adjacency[node]: ` ` ` ` ` `# If not visited ` ` ` `if` `(vis[it] ` `=` `=` `False` `): ` ` ` `dfs(it, leaf, vis) ` ` ` `leaf[node] ` `+` `=` `leaf[it] ` ` ` ` ` `if` `(` `len` `(adjacency[node]) ` `=` `=` `0` `): ` ` ` `leaf[node] ` `=` `1` ` ` `# Function to prnumber of ` `# leaf nodes of a node ` `def` `printLeaf(n, leaf): ` ` ` ` ` `# Function to prleaf nodes ` ` ` `for` `i ` `in` `range` `(` `1` `, n ` `+` `1` `): ` ` ` `print` `(` `"The node"` `, i, ` `"has"` `, ` ` ` `leaf[i], ` `"leaf nodes"` `) ` ` ` `# Driver Code ` ` ` `# Given N-ary Tree ` `''' ` `/* 1 ` ` ` `/ \ ` ` ` `2 3 ` ` ` `/ | \ ` ` ` `4 5 6 '''` ` ` `N ` `=` `6` `# no of nodes ` `# adjacency list for tree ` ` ` `insert(` `1` `, ` `2` `) ` `insert(` `1` `, ` `3` `) ` `insert(` `3` `, ` `4` `) ` `insert(` `3` `, ` `5` `) ` `insert(` `3` `, ` `6` `) ` ` ` `# Store count of leaf in subtree of i ` `leaf ` `=` `[` `0` `for` `i ` `in` `range` `(N ` `+` `1` `)] ` ` ` `# mark nodes visited ` `vis ` `=` `[` `0` `for` `i ` `in` `range` `(N ` `+` `1` `)] ` ` ` `dfs(` `1` `, leaf, vis) ` ` ` `printLeaf(N, leaf) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Convert a Binary Tree such that every node stores the sum of all nodes in its right subtree
- Change a Binary Tree so that every node stores sum of all nodes in left subtree
- Print the nodes of binary tree as they become the leaf node
- Implementing a BST where every node stores the maximum number of nodes in the path till any leaf
- Check if two nodes are in same subtree of the root node
- Subtree of all nodes in a tree using DFS
- Find the Kth node in the DFS traversal of a given subtree in a Tree
- Print all nodes that are at distance k from a leaf node
- Sum of nodes on the longest path from root to leaf node
- Closest leaf to a given node in Binary Tree
- Delete the last leaf node in a Binary Tree
- Sum of all leaf nodes of binary tree
- Print all leaf nodes of an n-ary tree using DFS
- Deepest left leaf node in a binary tree
- Product of all leaf nodes of binary tree

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.