How to build binary search tree from level order traversal

problem tree left view

Hey @dineshjani
The steps are:

  1. First pick the first element of the array and make it root.

  2. Pick the second element, if it’s value is smaller than root node value make it left child,

  3. Else make it right child

  4. Now recursively call the step (2) and step (3) to make a BST from its level Order Traversal.

    Node *LevelOrder(Node *root , int data)
    {
    if(root==NULL){
    root = getNode(data);
    return root;
    }
    if(data <= root->data)
    root->left = LevelOrder(root->left, data);
    else
    root->right = LevelOrder(root->right, data);
    return root;
    }

    Node* constructBst(int arr[], int n)
    {
    if(n==0)return NULL;
    Node *root =NULL;

     for(int i=0;i<n;i++) 
     root = LevelOrder(root , arr[i]); 
       
     return root; 
    

    }

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.