# 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 6Input:edge[][] = {{1, 5}, {1, 7}, {5, 6}}Output:6 7

**Approach:** DFS can be used to traverse the complete tree. We will keep track of parent while traversing to avoid the visited node array. Initially for every node we can set a flag and if the node have at least one child (i.e. non-leaf node) then we will reset the flag. The nodes with no children are the leaf nodes.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to perform DFS on the tree ` `void` `dfs(list<` `int` `> t[], ` `int` `node, ` `int` `parent) ` `{ ` ` ` `int` `flag = 1; ` ` ` ` ` `// Iterating the children of current node ` ` ` `for` `(` `auto` `ir : t[node]) { ` ` ` ` ` `// There is at least a child ` ` ` `// of the current node ` ` ` `if` `(ir != parent) { ` ` ` `flag = 0; ` ` ` `dfs(t, ir, node); ` ` ` `} ` ` ` `} ` ` ` ` ` `// Current node is connected to only ` ` ` `// its parent i.e. it is a leaf node ` ` ` `if` `(flag == 1) ` ` ` `cout << node << ` `" "` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// Adjacency list ` ` ` `list<` `int` `> t[1005]; ` ` ` ` ` `// List of all edges ` ` ` `pair<` `int` `, ` `int` `> edges[] = { { 1, 2 }, ` ` ` `{ 1, 3 }, ` ` ` `{ 2, 4 }, ` ` ` `{ 3, 5 }, ` ` ` `{ 3, 6 }, ` ` ` `{ 3, 7 }, ` ` ` `{ 6, 8 } }; ` ` ` ` ` `// Count of edges ` ` ` `int` `cnt = ` `sizeof` `(edges) / ` `sizeof` `(edges[0]); ` ` ` ` ` `// Number of nodes ` ` ` `int` `node = cnt + 1; ` ` ` ` ` `// Create the tree ` ` ` `for` `(` `int` `i = 0; i < cnt; i++) { ` ` ` `t[edges[i].first].push_back(edges[i].second); ` ` ` `t[edges[i].second].push_back(edges[i].first); ` ` ` `} ` ` ` ` ` `// Function call ` ` ` `dfs(t, 1, 0); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of the approach ` `t ` `=` `[[] ` `for` `i ` `in` `range` `(` `1005` `)] ` ` ` `# Function to perform DFS on the tree ` `def` `dfs(node, parent): ` ` ` `flag ` `=` `1` ` ` ` ` `# Iterating the children of current node ` ` ` `for` `ir ` `in` `t[node]: ` ` ` ` ` `# There is at least a child ` ` ` `# of the current node ` ` ` `if` `(ir !` `=` `parent): ` ` ` `flag ` `=` `0` ` ` `dfs(ir, node) ` ` ` ` ` `# Current node is connected to only ` ` ` `# its parent i.e. it is a leaf node ` ` ` `if` `(flag ` `=` `=` `1` `): ` ` ` `print` `(node, end ` `=` `" "` `) ` ` ` `# Driver code ` ` ` `# List of all edges ` `edges ` `=` `[[ ` `1` `, ` `2` `], ` ` ` `[ ` `1` `, ` `3` `], ` ` ` `[ ` `2` `, ` `4` `], ` ` ` `[ ` `3` `, ` `5` `], ` ` ` `[ ` `3` `, ` `6` `], ` ` ` `[ ` `3` `, ` `7` `], ` ` ` `[ ` `6` `, ` `8` `]] ` ` ` `# Count of edges ` `cnt ` `=` `len` `(edges) ` ` ` `# Number of nodes ` `node ` `=` `cnt ` `+` `1` ` ` `# Create the tree ` `for` `i ` `in` `range` `(cnt): ` ` ` `t[edges[i][` `0` `]].append(edges[i][` `1` `]) ` ` ` `t[edges[i][` `1` `]].append(edges[i][` `0` `]) ` ` ` `# Function call ` `dfs(` `1` `, ` `0` `) ` ` ` `# This code is contributed by Mohit Kumar ` |

*chevron_right*

*filter_none*

**Output:**

4 5 8 7

## Recommended Posts:

- Print all leaf nodes of a binary tree from right to left
- Print Sum and Product of all Non-Leaf nodes in Binary Tree
- Print the nodes of binary tree as they become the leaf node
- Print all leaf nodes of a Binary Tree from left to right
- Print leaf nodes in binary tree from left to right using one stack
- Print All Leaf Nodes of a Binary Tree from left to right | Set-2 ( Iterative Approach )
- Print the longest leaf to leaf path in a Binary tree
- Print Leaf Nodes at a given Level
- Sum of all leaf nodes of binary tree
- Print all nodes that are at distance k from a leaf node
- Determine the count of Leaf nodes in an N-ary tree
- Count Non-Leaf nodes in a Binary Tree
- Product of all leaf nodes of binary tree
- Number of leaf nodes in the subtree of every node of an n-ary tree
- Pairwise Swap leaf nodes in a 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.