Adding longest tree function