it 's a leaf, so print the ancestor at distance k only  if the ancestor is not already printed
if ( node . left == null && node . right == null && ( pathLen - k - 1 ) >= 0 && visited [ pathLen - k - 1 ] == false ) { document . write ( path [ pathLen - k - 1 ] + " " ) ; visited [ pathLen - k - 1 ] = true ; return ; }