flatten trees iterative