Print all leaf nodes of an n-ary tree using DFS
Last Updated :
05 Aug, 2021
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 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++
#include <bits/stdc++.h>
using namespace std;
void dfs(list< int > t[], int node, int parent)
{
int flag = 1;
for ( auto ir : t[node]) {
if (ir != parent) {
flag = 0;
dfs(t, ir, node);
}
}
if (flag == 1)
cout << node << " " ;
}
int main()
{
list< int > t[1005];
pair< int , int > edges[] = { { 1, 2 },
{ 1, 3 },
{ 2, 4 },
{ 3, 5 },
{ 3, 6 },
{ 3, 7 },
{ 6, 8 } };
int cnt = sizeof (edges) / sizeof (edges[0]);
int node = cnt + 1;
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);
}
dfs(t, 1, 0);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static class pair
{
int first,second;
pair( int a, int b)
{
first = a;
second = b;
}
}
static void dfs(Vector t, int node, int parent)
{
int flag = 1 ;
for ( int i = 0 ; i < ((Vector)t.get(node)).size(); i++)
{
int ir = ( int )((Vector)t.get(node)).get(i);
if (ir != parent)
{
flag = 0 ;
dfs(t, ir, node);
}
}
if (flag == 1 )
System.out.print( node + " " );
}
public static void main(String args[])
{
Vector t = new Vector();
pair edges[] = { new pair( 1 , 2 ),
new pair( 1 , 3 ),
new pair( 2 , 4 ),
new pair( 3 , 5 ),
new pair( 3 , 6 ),
new pair( 3 , 7 ),
new pair( 6 , 8 ) };
int cnt = edges.length;
int node = cnt + 1 ;
for ( int i = 0 ; i < 1005 ; i++)
{
t.add( new Vector());
}
for ( int i = 0 ; i < cnt; i++)
{
((Vector)t.get(edges[i].first)).add(edges[i].second);
((Vector)t.get(edges[i].second)).add(edges[i].first);
}
dfs(t, 1 , 0 );
}
}
|
Python3
t = [[] for i in range ( 1005 )]
def dfs(node, parent):
flag = 1
for ir in t[node]:
if (ir ! = parent):
flag = 0
dfs(ir, node)
if (flag = = 1 ):
print (node, end = " " )
edges = [[ 1 , 2 ],
[ 1 , 3 ],
[ 2 , 4 ],
[ 3 , 5 ],
[ 3 , 6 ],
[ 3 , 7 ],
[ 6 , 8 ]]
cnt = len (edges)
node = cnt + 1
for i in range (cnt):
t[edges[i][ 0 ]].append(edges[i][ 1 ])
t[edges[i][ 1 ]].append(edges[i][ 0 ])
dfs( 1 , 0 )
|
C#
using System.Collections;
using System.Collections.Generic;
using System;
class GFG{
class pair
{
public int first, second;
public pair( int a, int b)
{
first = a;
second = b;
}
}
static void dfs(ArrayList t, int node,
int parent)
{
int flag = 1;
for ( int i = 0;
i < ((ArrayList)t[node]).Count;
i++)
{
int ir = ( int )((ArrayList)t[node])[i];
if (ir != parent)
{
flag = 0;
dfs(t, ir, node);
}
}
if (flag == 1)
Console.Write( node + " " );
}
public static void Main( string []args)
{
ArrayList t = new ArrayList();
pair []edges = { new pair(1, 2),
new pair(1, 3),
new pair(2, 4),
new pair(3, 5),
new pair(3, 6),
new pair(3, 7),
new pair(6, 8) };
int cnt = edges.Length;
for ( int i = 0; i < 1005; i++)
{
t.Add( new ArrayList());
}
for ( int i = 0; i < cnt; i++)
{
((ArrayList)t[edges[i].first]).Add(
edges[i].second);
((ArrayList)t[edges[i].second]).Add(
edges[i].first);
}
dfs(t, 1, 0);
}
}
|
Javascript
<script>
function dfs(t, node, parent)
{
let flag = 1;
for (let i = 0; i < t[node].length; i++)
{
let ir = t[node][i];
if (ir != parent)
{
flag = 0;
dfs(t, ir, node);
}
}
if (flag == 1)
document.write( node + " " );
}
let t = []
let edges = [ [ 1, 2 ], [ 1, 3 ],
[ 2, 4 ], [ 3, 5 ],
[ 3, 6 ], [ 3, 7 ],
[ 6, 8 ] ];
let cnt = edges.length;
let node = cnt + 1;
for (let i = 0; i < 1005; i++)
{
t.push([]);
}
for (let i = 0; i < cnt; i++)
{
t[edges[i][0]].push(edges[i][1])
t[edges[i][1]].push(edges[i][0])
}
dfs(t, 1, 0);
</script>
|
Time Complexity: O(N), where N is the number of nodes in the graph.
Auxiliary Space: O(N)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...