I am trying to build bst from preorder traversal in this code but only the left subtree is getting built.Can you please help me understand my mistake?
Can't build full bst from the preorder array
@Sayon-Palit-2873308702757758 can you share the approach you are using to build the tree? There is a video in the course covering how to build a BST from an array, you can refer to that also.
That video just says to find your own solution
My approach is to iterate over the array , in the beginning just insert element as root ,next the left node must be b/w INT_MIN and the root , and right will be between root and INT_MAX and recursively build the tree using this
hi @Sayon-Palit-2873308702757758 look for it in the concepts there must be a video explaining how to build a BST.
The approach is to, iterate over the array and insert each element individually.
For insertion, if root is NULL, make a node and return that node.
else, if the element is smaller or equal to the root node, insert in left part or else insert it in the right part.
yes that is the soln for a normal array but since this is preorder it may become a skewed tree in cases like 1 2 3 4 …
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.