• + 0 comments

    Good approach is to just simulate preorder traversal. Since we know how the sequence is built we try to consume the elements in the order they are given to use. If the next element does not fit into our contraints of lo (lower bound) and hi (upper bound) then we propagate to the parent.

    As the result, running preorder on a valid sequence will consume all elements, otherwise if the sequence could not be fully consumed a non-empty sequence of the remaining elements will be returned.

    This could be easily mapped to YES/NO

    preorder [] lo hi = []
    preorder (x:xs) lo hi = if lo < x && x < hi then rest else x:xs
        where remain = preorder xs lo x
              rest = preorder remain x hi