You need to know a equation to
solve this problem ( Catalen number).
A non empty binary tree consists of
a root X, a left sub-tree L, and a right sub-tree R. Let n be
the size of the binary tree, let nL= | L | the size of L, nR = |
R | = the size of R. Then n = 1 + nL + nR. So there are only n
different possible of pair (nL,nR). Suppose if n=6, the only
possibility are (0,5),(1,4),(2,3),(3,2),(4,1) or (5,0). So in
the closed form the formula is,
f(n)
=
First you find all the tree type for all
nodes 1 to 25 ( I am talking about pre-calculation), because 25
is enough for the given input.
Remember for 14 the output is 4 and
for 13 the output is also 4. |