Rebuild a binary tree from preorder and inorder lists

后端 未结 2 652
夕颜
夕颜 2021-01-26 00:05

Hi I\'m trying to rebuild a binary tree, I almost got it, except it throws me an error and I don\'t know why

buildTree :: (Ord a, Eq a) => [a] -> [a] ->         


        
2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-26 00:51

    The runtime is failing on this line:

    Just rootInd = elemIndex root inOrd
    

    elemIndex is returning Nothing when running your example input, but your code says it will always return a Just, so the runtime crashes. You need to handle the case where elemIndex root inOrd returns Nothing.

    Perhaps more importantly, you should enable all warnings with the -Wall flag to show up as compiler errors so that your code wouldn't compile to begin with.

提交回复
热议问题