The code suppose to return a list of every path from a root to a leaf in the tree, in order from left to right. Nodes with one child (one Tip
and one Bin
) are not considered leaf nodes.
I tried to code like
paths :: Tree a -> [[a]]
paths Tip = [[]]
paths (Bin Tip x Tip) = [[x]]
paths (Bin left x right) = map (x:) (paths left ++ paths right)
But it seems return the path include nodes with one child. This will lead to paths like [[4,3],[4]]
on Bin (Bin Tip 3 Tip) 4 Tip
.
[] :: [a]
is the empty list for all list types, including nested list types (wherea ~ [b]
). – chepner