[Solved] Maximum tree depth in Haskell

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 [???] … Read more

[Solved] Traverse Non-Binary Tree [closed]

def traverse(tree_of_lists): for item in tree_of_lists: if isinstance(item, list): for x in traverse(item): yield x else: yield item This is the “basic” solution — can run in Python 2.7 and gives you an iterable that you can simply loop on. (In recent Python 3.* versions you’d use yield from item instead of the inner for … Read more

[Solved] How do I trace the progress of this tree code?

Without tracing, it’s relatively straightforward to see what a given tree should print. The structure of the method means that a string will look like this: <content><left sub-tree><content><right sub-tree><content> So all you have to do is continually substitute that pattern for the left and right sub-trees (with empty strings for non-existent sub-trees) and get the … Read more