No, you don't have to sort it.
In the post-order traversal you can find some hidden info. Like:
- The last element is always the tree root
- All the previous elements greater than the root are part of the right subtree. All the elements lesser are part of the left subtree.
- You can apply the above rules recursively.
This is only true because you have only distinct elements. If there were repetitions, the tree for the traversal couldn't be unique.
Here's an example implementation (in Python) that shows this:
from collections import deque
def visit(in_queue, limit = None):
if len(in_queue) == 0 or in_queue[-1] < limit:
return None
root = in_queue.pop()
right = visit(in_queue, max(root, limit))
left = visit(in_queue, limit)
if left is None and right is None:
return root
else:
return (left, root, right)
def print_graphviz(tree):
if not isinstance(tree, tuple):
return tree
left = print_graphviz(tree[0])
right = print_graphviz(tree[2])
if left is not None: print tree[1], '->', left
if right is not None: print tree[1], '->', right
return tree[1]
def make_tree(post_order):
return visit(deque(post_order))
print 'digraph {'
print print_graphviz(make_tree([0, 2, 1, 4, 3, 6, 8, 7, 5]))
print '}'
Run like this:
python test.py | dot -Tpng | display
And you'll have a nice tree output:

http://www.geeksforgeeks.org/construct-bst-from-given-preorder-traversa/- Marichyasana