Driver code
let g = new Array ( 100005 ) ; for ( let i = 0 ; i < g . length ; i ++ ) g [ i ] = [ ] ; let edges = 6 ; g [ 18 ] . push ( 7 ) ; g [ 7 ] . push ( 18 ) ; g [ 18 ] . push ( 15 ) ; g [ 15 ] . push ( 18 ) ; g [ 15 ] . push ( 2 ) ; g [ 2 ] . push ( 15 ) ; g [ 7 ] . push ( 4 ) ; g [ 4 ] . push ( 7 ) ; g [ 7 ] . push ( 12 ) ; g [ 12 ] . push ( 7 ) ; g [ 12 ] . push ( 9 ) ; g [ 9 ] . push ( 12 ) ; let root = 18 ; document . write ( dfs ( g , root , - 1 ) ) ;