I am very confused by a number of articles at different sites regarding constructing a Binary Search Tree
from any one traversal (pre
,post
or in-order
), or a combination of any two of them. For example, at this page, it says that given the pre
,post
or level
order traversal, along with the in-order
traversal, one can construct the BST
. But here and there, they show us to construct a BST
from pre-order
alone. Also, here they show us how to construct the BST
from given pre
and post-order
traversals. In some other site, I found a solution for constructing a BST
from the post-order
traversal only.
Now I know that given the inorder
and pre-order
traversals, it is possible to uniquely form a BST
. As regards the first link I provided, although they say that we can't construct the BST
from pre-order
and post-order
, can't I just sort the post-order
array to get its inorder
traversal, and then use that and the pre-order
array to form the BST
? Will that be same as the solution in the 4th link, or different? And given pre-order
only, I can sort that to get the in-order
, then use that and the pre-order
to get the BST
. Again, does that have to be different from the solution at links 2 and 3?
Specifically, what is sufficient to uniquely generate the BST
? If uniquement is not required, then I can simply sort it to get the in-order
traversal, and build one of the N possible BST
s from it recursively.