title: Constructing a binary tree from a list of its edges (node pairs)
body: <p>I'd like to construct a binary tree from a quite unusual input. The input contains: </p>
  
  <ol>
  <li><p>Total number of nodes.</p></li>
  <li><p>The integer label of the root.</p></li>
  <li><p>A list of all edges (vertices/nodes that are connected to each
  other). The edges in the list are UNSORTED, there is only one rule for
  determining left/right children - the child in the edge that appears
  first in list is always on the left. The order of child/parent in the vertices pair is also random.</p></li>
  </ol>
  
  <p>I've come up with some straighforward solutions but they require multiple searches through the list of all edges (I'd basically find the 2 edges that have the labeled root in them and repeat this process for all the subtrees.) </p>
  
  <p>I imagine this straightforward approach would be <strong>VERY inefficient</strong> for trees with a big amount of nodes, but I can't come up with anything else.</p>
  
  <p>Any ideas for more efficient algorithms to solve this?</p>
  
  <p>Here's an <strong>example</strong> for better visualization:</p>
  
  <p><em>INPUT: 5 NODES, ROOT LABELED 2, LIST OF EDGES: [(1,0),(1,2),(2,3),(1,4)]</em></p>
  
  <p>The tree would look like this:</p>
  
  <pre><code>        2
      1       3
   0     4
  </code></pre>