Simplify `transformTree` by using `lastIndexOf`