You would want to write a recursive function to do this. For each Tree
constructor, you’ll need a different case in your function. To start with, you know that the depth of any Leaf
is 1
, so
maxDepth :: Tree -> Int
maxDepth (Leaf _) = 1
maxDepth (Branch2 c left right) = maximum [???]
maxDepth (Branch3 c left center right) = maximum [???]
I’ll let you finish the rest of the function. You could do it a few different ways as well (such as using max
instead of maximum
).
9
solved Maximum tree depth in Haskell