rearange input tree