Fix memory leak in tree