A tree consisting of n nodes is given, we need to print its DFS.
Examples :
Input : Edges of graph
1 2
1 3
2 4
3 5
Output : 1 2 4 3 5
A simple solution is to do implement standard DFS.
We can modify our approach to avoid extra space for visited nodes. Instead of using the visited array, we can keep track of parent. We traverse all adjacent nodes but the parent.
Below is the implementation :
C++
#include <bits/stdc++.h>
using namespace std;
void dfs(vector< int > list[], int node, int arrival)
{
cout << node << '\n' ;
for ( int i = 0; i < list[node].size(); i++) {
if (list[node][i] != arrival)
dfs(list, list[node][i], node);
}
}
int main()
{
int nodes = 5;
vector< int > list[10000];
list[1].push_back(2);
list[2].push_back(1);
list[1].push_back(3);
list[3].push_back(1);
list[2].push_back(4);
list[4].push_back(2);
list[3].push_back(5);
list[5].push_back(3);
dfs(list, 1, 0);
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void dfs(LinkedList<Integer> list[],
int node, int arrival)
{
System.out.println(node);
for ( int i = 0 ; i < list[node].size(); i++) {
if (list[node].get(i) != arrival)
dfs(list, list[node].get(i), node);
}
}
public static void main(String[] args)
{
int nodes = 5 ;
LinkedList<Integer> list[] = new LinkedList[nodes+ 1 ];
for ( int i = 0 ; i < list.length; i ++){
list[i] = new LinkedList<Integer>();
}
list[ 1 ].add( 2 );
list[ 2 ].add( 1 );
list[ 1 ].add( 3 );
list[ 3 ].add( 1 );
list[ 2 ].add( 4 );
list[ 4 ].add( 2 );
list[ 3 ].add( 5 );
list[ 5 ].add( 3 );
dfs(list, 1 , 0 );
}
}
|
Python3
def dfs( List , node, arrival):
print (node)
for i in range ( len ( List [node])):
if ( List [node][i] ! = arrival):
dfs( List , List [node][i], node)
if __name__ = = '__main__' :
nodes = 5
List = [[] for i in range ( 10000 )]
List [ 1 ].append( 2 )
List [ 2 ].append( 1 )
List [ 1 ].append( 3 )
List [ 3 ].append( 1 )
List [ 2 ].append( 4 )
List [ 4 ].append( 2 )
List [ 3 ].append( 5 )
List [ 5 ].append( 3 )
dfs( List , 1 , 0 )
|
C#
using System;
using System.Collections.Generic;
public class GFG {
public static void dfs(List< int > []list,
int node, int arrival)
{
Console.WriteLine(node);
for ( int i = 0; i < list[node].Count; i++) {
if (list[node][i] != arrival)
dfs(list, list[node][i], node);
}
}
public static void Main(String[] args)
{
int nodes = 5;
List< int > []list = new List< int >[nodes+1];
for ( int i = 0; i < list.Length; i ++){
list[i] = new List< int >();
}
list[1].Add(2);
list[2].Add(1);
list[1].Add(3);
list[3].Add(1);
list[2].Add(4);
list[4].Add(2);
list[3].Add(5);
list[5].Add(3);
dfs(list, 1, 0);
}
}
|
Javascript
<script>
function dfs(list, node, arrival)
{
document.write(node + "</br>" );
for (let i = 0; i < list[node].length; i++) {
if (list[node][i] != arrival)
dfs(list, list[node][i], node);
}
}
let nodes = 5;
let list = new Array(nodes+1);
for (let i = 0; i < list.length; i ++){
list[i] = [];
}
list[1].push(2);
list[2].push(1);
list[1].push(3);
list[3].push(1);
list[2].push(4);
list[4].push(2);
list[3].push(5);
list[5].push(3);
dfs(list, 1, 0);
</script>
|
Time Complexity: O(V+E) where V is the number of nodes and E is the number of edges in the graph.
Auxiliary space: O(10000)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!