title: Shortest branch in a binary tree?
body: <p>A binary tree can be encoded using two functions <code>l</code> and <code>r</code>
  such that for a <code>node n</code>, <code>l(n)</code> give the left child of <code>n</code>, <code>r(n)</code>
  give the right child of <code>n</code>.</p>
  
  <p>A branch of a tree is a path from the root to a leaf, the
  length of a branch to a particular leaf is the number of
  arcs on the path from the root to that leaf.</p>
  
  <p>Let <code>MinBranch(l,r,x)</code> be a simple recursive algorithm for
  taking a binary tree encoded by the l and r functions
  together with the root node x for the binary tree and
  returns the length of the shortest branch of the binary
  tree.</p>
  
  <p>Give the pseudocode for this algorithm.</p>
  
  <p>OK, so basically this is what I've come up with so far:</p>
  
  <pre><code>MinBranch(l, r, x)
  {
      if x is None return 0
  
      left_one = MinBranch(l, r, l(x))
  
      right_one = MinBranch(l, r, r(x))
  
      return {min (left_one),(right_one)}
  }
  </code></pre>
  
  <p>Obviously this isn't great or perfect. I'd be greatful if
  people can help me get this perfect and working - any help
  will be appreciated.</p>