Flatten tree