I have this function that i wrote to pass from a tree to a list in Haskell:
treeToList :: Tree -> [Int]
treeToList (Leaf x) = [x]
treeToList (Node left x right) = treeToList left ++ [x] ++ treeToList right
This works just fine, however i have a doubt:
With the input:
treeToList (Node (Leaf 1) 2 (Node (Leaf 3) 4 (Leaf 5)))
the function produces this list:
[1,2,3,4,5]
which, i think is wrong, because if i want to go the other way around, and write the tree from the list, i'm going to write it wrong.
How can i fix this ?