```haskell invert :: Tree a -> Tree a invert (Leaf x) = Leaf x invert t@(Node _ _) = Node (invert (pick False t)) (invert (pick True t)) where -- pick b t = subtree consisting of exactly the leaves whose *last* step is b, -- with that last step dropped (so the depth decreases by 1). pick :: Bool -> Tree a -> Tree a pick _ (Leaf x) = Leaf x -- not expected for perfect trees, but keeps the function total pick b (Node l r) = case (l, r) of (Leaf x, Leaf y) -> if b then Leaf y else Leaf x _ -> Node (pick b l) (pick b r) ``` (If you want the provided `main` to compile, make sure `Tree` derives `Show`, e.g. `data Tree a = ... deriving (Show, Eq)`.)