using fold to fill in the tree