DFS for a n | DFS on tree ; Printing traversed node ; Traversing adjacent edges ; Not traversing the parent node ; Driver Code ; Number of nodes ; Adjacency List ; Designing the tree ; Function call
def dfs ( List , node , arrival ) : NEW_LINE INDENT print ( node ) NEW_LINE for i in range ( len ( List [ node ] ) ) : NEW_LINE INDENT if ( List [ node ] [ i ] != arrival ) : NEW_LINE INDENT dfs ( List , List [ node ] [ i ] , node ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT nodes = 5 NEW_LINE List = [ [ ] for i in range ( 10000 ) ] NEW_LINE List [ 1 ] . append ( 2 ) NEW_LINE List [ 2 ] . append ( 1 ) NEW_LINE List [ 1 ] . append ( 3 ) NEW_LINE List [ 3 ] . append ( 1 ) NEW_LINE List [ 2 ] . append ( 4 ) NEW_LINE List [ 4 ] . append ( 2 ) NEW_LINE List [ 3 ] . append ( 5 ) NEW_LINE List [ 5 ] . append ( 3 ) NEW_LINE dfs ( List , 1 , 0 ) NEW_LINE DEDENT