flatten a tree