Superintegrability and associated polynomial solutions: Euclidean space and the sphere in two dimensions