flatten tree