List to tree implementation