title: Depth of a tree (Haskell)
body: <p>I'm trying to figure out how to calculate the depth of a general tree in Haskell. I can figure out the solution for simple binary trees, but not for general trees with any number of leaves.</p>
  
  <p>Here's the code I have for binary trees.</p>
  
  <pre><code>--depth of a binary tree.
  
  depth :: Tree a -&gt; Int
  depth Nil            = 0
  depth (Node n x1 x2) = 1 + max (depth x1) (depth x2)
  </code></pre>
  
  <p>How can I modify this for general trees? General trees contain a list of trees, and that is where I am encountering difficulty. </p>
  
  <p>Secondly, I want to turn the tree into a list (so I can do operations such as calculating the sum, etc.)</p>
  
  <p>Again, I can figure it out for binary trees but not for general trees. </p>
  
  <pre><code>--tree into a list.
  
  treeToList:: Tree a -&gt; [a]
  treeToList Nil = []
  treeToList (Node n x1 x2)
           = collapse x1 ++ [n] ++ collapse x2
  </code></pre>