 <p>Well the algorithm is as follows:</p> <p>Let, <code>P(N)</code> denote the number of trees possible with <code>N</code> nodes. Let the indexes of the nodes be <code>1,2,3,...</code></p> <p>Now, lets pick the root of the tree. Any of the given <code>N</code> nodes can be the root. Say node <code>i</code> has been picked as root. Then, all the elements to the left of <code>i</code> in the inorder sequence must be in the <code>left sub-tree</code>. Similarly, to the right.</p> <p>So, total possibilities are: <code>P(i-1)*P(N-i)</code></p> <p>In the above expression <code>i</code> varies from <code>1 to N</code>.</p> <p>Hence we have,</p> <pre><code>P(N) = P(0)*P(N-1) + P(1)*P(N-2) + P(2)*P(N-3).... </code></pre> <p>The base cases will be:</p> <pre><code>P(0) = 1 P(1) = 1 </code></pre> <p>Thus this can be solved by using <code>Dynamic Programming</code>.</p>