Reconstruir un árbol binario a partir de listas de preorden y orden
Hola, estoy tratando de reconstruir un árbol binario, casi lo tengo, excepto que me arroja un error y no sé por qué
buildTree :: (Ord a, Eq a) => [a] -> [a] -> Tree a
buildTree [] [] = Empty
buildTree preOrd inOrd = Node root left right
where root = head preOrd
left = buildTree leftPreOrd leftInOrd
right = buildTree rigthPreOrd leftInOrd
Just rootInd = elemIndex root inOrd
leftPreOrd = tail (take (rootInd + 1) preOrd)
rigthPreOrd = tail (drop rootInd preOrd)
leftInOrd = take rootInd inOrd
rightInord = drop (rootInd + 1) inOrd
Cuando lo llamo usando
buildTree [10,5,2,6,14,12,15] [2,5,6,10,12,14,15]
me arroja esto:
Exception: reconstruir.hs:26:11-45: Irrefutable pattern failed for pattern Just rootInd