You’re right. The condition should be

(has m (Node v l Nil) && has n (Node v Nil r)) || (has n (Node v l Nil) && has m (Node v Nil r))

since switching from a binary search tree to a binary tree removes the guarantee that m will be to the left of n.

]]>in the case, we assume that the tree is not a bineary search tree, if m is r and n is l, you have to return v, but in your solution it goes in the otherwise-branch. or am I missing something? ]]>

import Control.Applicative data BinTree a = Node a (BinTree a) (BinTree a) | Nil deriving (Eq, Show) lca :: Ord a => a -> a -> BinTree a -> Maybe a lca _ _ Nil = Nothing lca m n (Node v l r) | has m (Node v l Nil) && has n (Node v Nil r) = Just v | otherwise = lca m n l <|> lca m n r has :: Eq a => a -> BinTree a -> Bool has _ Nil = False has e (Node v l r) = v == e || has e l || has e r]]>

See what happens when you change the 7 to a 17 without changing anything else (except the node value and the first test content).

]]>