Given preorder traversal array of a BST, recontruct the BST.
---------------------------------------------------------------
I like this solution!!!!!!!!!!
This can be solved in O(n) only. The approach am using is, keep a separate stack of node pointers. Push on the 1st node. Keep on traversing the preorder traversal
1. if the value of stack top is more than the current node value, then make the current node left pointer of stack top.
2. if the value of stack top is less that current node value, keep popping from the stack till value of stack top is more than current node. Then make current node the right child of last popped element.
Push the current node on stack in both the cases.
It may seem like an O(n^2) algo, but we are pushing and popping every element on stack only once so this is O(n) time and space.
Please check ideone.com/VohnS for details