Construct BST from its given level order traversal

Last Updated : 4 May, 2026

Given an array arr[] representing the level order traversal of a BST, and we have to construct the BST from it.

Example: 

Input: arr[] = [7, 4, 12, 3, 6, 8, 1, 5, 10]
Output: BST: 
           

Construct-BST-from-its-given-level-order-traversal
Try It Yourself
redirect icon

[Naive Approach] Using Recursion - O(n * h) space and O(n) space

The idea is to create an empty BST and insert each element from the level order array one by one using the BST insertion property, where smaller elements go to the left subtree and larger elements go to the right subtree.

Steps to solve the problem:

  • Start with an empty root node.
  • For each value in the level order array, insert it into the BST using the following rules:
  • If the current node is null, create a new node with the given value and return it.
  • If the value is smaller than the current node’s value, recursively insert it into the left subtree.
  • If the value is larger than the current node’s value, recursively insert it into the right subtree
C++
// C++ program to Construct BST from 
// its given level order traversal
#include <iostream>
#include<vector>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;
    Node(int val) {
        data = val;
        left = nullptr; 
        right = nullptr;
    }
};

Node* insertIntoBST(Node* root, int val) {
    
    // If node is null, then we can insert 
    // val here.
    if (!root) return new Node(val);
    
    // If value is less than root value, 
    // insert into left subtree.
    if (val < root->data) {
        root->left = insertIntoBST(root->left, val);
    } 
    
    // Else, insert into right subtree.
    else {
        root->right = insertIntoBST(root->right, val);
    }
    
    return root;
}

Node* constructBst(vector<int> &arr) {
    int n = arr.size();
    
    Node* root = nullptr;
    
    // Insert each value one by one 
    for (int i = 0; i < n; i++) {
        root = insertIntoBST(root, arr[i]);
    }
    return root;
}

void preOrder(Node* root) {
    if (!root) return;
    
    cout << root->data << " ";
    preOrder(root->left);
    preOrder(root->right);
}

int main() {
    vector<int> arr = {7, 4, 12, 3, 6, 8, 1, 5, 10};
    Node* root = constructBst(arr);
    preOrder(root);
    return 0;
}
Java
// Java program to Construct BST from 
// its given level order traversal

class Node {
    int data;
    Node left;
    Node right;
    Node(int val) {
        data = val;
        left = null; 
        right = null;
    }
}

class GfG {

    // If node is null, then we can insert 
    // val here.
    static Node insertIntoBST(Node root, int val) {
        if (root == null) return new Node(val);

        // If value is less than root value, 
        // insert into left subtree.
        if (val < root.data) {
            root.left = insertIntoBST(root.left, val);
        } 

        // Else, insert into right subtree.
        else {
            root.right = insertIntoBST(root.right, val);
        }

        return root;
    }

    static Node constructBst(int[] arr) {
        int n = arr.length;
        Node root = null;

        // Insert each value one by one 
        for (int i = 0; i < n; i++) {
            root = insertIntoBST(root, arr[i]);
        }
        return root;
    }

    static void preOrder(Node root) {
        if (root == null) return;

        System.out.print(root.data + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public static void main(String[] args) {
        int[] arr = {7, 4, 12, 3, 6, 8, 1, 5, 10};
        Node root = constructBst(arr);
        preOrder(root);
    }
}
Python
# Python program to Construct BST from 
# its given level order traversal

class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None

# If node is null, then we can insert 
# val here.
def insertIntoBST(root, val):
    if not root:
        return Node(val)

    # If value is less than root value, 
    # insert into left subtree.
    if val < root.data:
        root.left = insertIntoBST(root.left, val)

    # Else, insert into right subtree.
    else:
        root.right = insertIntoBST(root.right, val)

    return root

def constructBst(arr):
    n = len(arr)
    root = None

    # Insert each value one by one 
    for i in range(n):
        root = insertIntoBST(root, arr[i])
    return root

def preOrder(root):
    if not root:
        return

    print(root.data, end=" ")
    preOrder(root.left)
    preOrder(root.right)

if __name__ == "__main__":
    arr = [7, 4, 12, 3, 6, 8, 1, 5, 10]
    root = constructBst(arr)
    preOrder(root)
C#
// C# program to Construct BST from 
// its given level order traversal
using System;

class Node {
    public int data;
    public Node left;
    public Node right;

    public Node(int val) {
        data = val;
        left = null;
        right = null;
    }
}

class GfG {

    // If node is null, then we can insert 
    // val here.
    static Node insertIntoBST(Node root, int val) {
        if (root == null) return new Node(val);

        // If value is less than root value, 
        // insert into left subtree.
        if (val < root.data) {
            root.left = insertIntoBST(root.left, val);
        } 

        // Else, insert into right subtree.
        else {
            root.right = insertIntoBST(root.right, val);
        }

        return root;
    }

    static Node constructBst(int[] arr) {
        int n = arr.Length;
        Node root = null;

        // Insert each value one by one 
        for (int i = 0; i < n; i++) {
            root = insertIntoBST(root, arr[i]);
        }
        return root;
    }

    static void preOrder(Node root) {
        if (root == null) return;

        Console.Write(root.data + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    static void Main(string[] args) {
        int[] arr = {7, 4, 12, 3, 6, 8, 1, 5, 10};
        Node root = constructBst(arr);
        preOrder(root);
    }
}
JavaScript
// JavaScript program to Construct BST from 
// its given level order traversal

class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

// If node is null, then we can insert 
// val here.
function insertIntoBST(root, val) {
    if (root === null) return new Node(val);

    // If value is less than root value, 
    // insert into left subtree.
    if (val < root.data) {
        root.left = insertIntoBST(root.left, val);
    } 

    // Else, insert into right subtree.
    else {
        root.right = insertIntoBST(root.right, val);
    }

    return root;
}

function constructBst(arr) {
    let n = arr.length;
    let root = null;

    // Insert each value one by one 
    for (let i = 0; i < n; i++) {
        root = insertIntoBST(root, arr[i]);
    }
    return root;
}

function preOrder(root) {
    if (root === null) return;

    process.stdout.write(root.data + " ");
    preOrder(root.left);
    preOrder(root.right);
}
// Driver Code
let arr = [7, 4, 12, 3, 6, 8, 1, 5, 10];
let root = constructBst(arr);
preOrder(root);

Output
7 4 3 1 6 5 12 8 10 

[Expected Approach] Using Level Order Traversal - O(n) time and O(n) space

The idea is to build the BST level by level using a queue, where each node carries a valid value range. New elements are inserted as left or right children only if they fall within the range, ensuring the BST property is maintained.

Steps to solve the problem:

  • Take the first element as root and push it into a queue with the value range (−∞, +∞).
  • For each next element in the level order array, look at the front node and check whether it can be placed in its left or right subtree based on the valid range.
  • If it fits on the left, create the left child, assign range (low, node->data − 1), and push it to the queue.
  • If it fits on the right, create the right child, assign range (node->data + 1, high), and push it to the queue.
  • Continue this process until all elements are inserted and the BST is fully constructed.
C++
// C++ program to Construct BST from 
// its given level order traversal
#include <iostream>
#include<vector>
#include<queue>
#include <climits>
using namespace std;

class Node {
public:
    int data;
    Node* left;
    Node* right;
    Node(int val) {
        data = val;
        left = nullptr; 
        right = nullptr;
    }
};

Node* constructBst(vector<int> &arr) {
    int n = arr.size();
    
    Node* root = new Node(arr[0]);
    queue<pair<Node*, pair<int, int>>> q;
    q.push({root, {INT_MIN, INT_MAX}});
    
    int i = 1;
    while (i < n && !q.empty()) {
        auto curr = q.front();
        q.pop();
        
        Node* node = curr.first;
        int low = curr.second.first;
        int high = curr.second.second;
        
        // Check for left child
        if (i < n && arr[i] >= low && arr[i] < node->data) {
            node->left = new Node(arr[i]);
            q.push({node->left, {low, node->data-1}});
            i++;
        }
        
        // Check for right child
        if (i < n && arr[i] > node->data && arr[i] <= high) {
            node->right = new Node(arr[i]);
            q.push({node->right, {node->data+1, high}});
            i++;
        }
    }
    return root;
}

void preOrder(Node* root) {
    if (!root) return;
    
    cout << root->data << " ";
    preOrder(root->left);
    preOrder(root->right);
}

int main() {
    vector<int> arr = {7, 4, 12, 3, 6, 8, 1, 5, 10};
    Node* root = constructBst(arr);
    preOrder(root);
    return 0;
}
Java
// Java program to Construct BST from 
// its given level order traversal
import java.util.*;

class Node {
    int data;
    Node left;
    Node right;
    Node(int val) {
        data = val;
        left = null; 
        right = null;
    }
}

class Pair<X, Y> {
    public final X first;
    public final Y second;
    public Pair(X first, Y second) {
        this.first = first;
        this.second = second;
    }
}

class GFG {
    static Node constructBst(int[] arr) {
        int n = arr.length;
        
        Node root = new Node(arr[0]);
        Queue<Pair<Node, Pair<Integer, Integer>>> q = new LinkedList<>();
        q.add(new Pair<>(root, new Pair<>(Integer.MIN_VALUE, Integer.MAX_VALUE)));
        
        int i = 1;
        while (i < n && !q.isEmpty()) {
            Pair<Node, Pair<Integer, Integer>> curr = q.poll();
            
            Node node = curr.first;
            int low = curr.second.first;
            int high = curr.second.second;
            
            // Check for left child
            if (i < n && arr[i] >= low && arr[i] < node.data) {
                node.left = new Node(arr[i]);
                q.add(new Pair<>(node.left, new Pair<>(low, node.data - 1)));
                i++;
            }
            
            // Check for right child
            if (i < n && arr[i] > node.data && arr[i] <= high) {
                node.right = new Node(arr[i]);
                q.add(new Pair<>(node.right, new Pair<>(node.data + 1, high)));
                i++;
            }
        }
        return root;
    }

    static void preOrder(Node root) {
        if (root == null) return;
        
        System.out.print(root.data + " ");
        preOrder(root.left);
        preOrder(root.right);
    }

    public static void main(String[] args) {
        int[] arr = {7, 4, 12, 3, 6, 8, 1, 5, 10};
        Node root = constructBst(arr);
        preOrder(root);
    }
}
Python
# Python program to Construct BST from 
# its given level order traversal
import sys
from collections import deque

class Node:
    def __init__(self, val):
        self.data = val
        self.left = None
        self.right = None

def constructBst(arr):
    n = len(arr)
    
    root = Node(arr[0])
    q = deque()
    q.append((root, (-sys.maxsize - 1, sys.maxsize)))
    
    i = 1
    while i < n and q:
        curr = q.popleft()
        
        node = curr[0]
        low = curr[1][0]
        high = curr[1][1]
        
        # Check for left child
        if i < n and arr[i] >= low and arr[i] < node.data:
            node.left = Node(arr[i])
            q.append((node.left, (low, node.data - 1)))
            i += 1
        
        # Check for right child
        if i < n and arr[i] > node.data and arr[i] <= high:
            node.right = Node(arr[i])
            q.append((node.right, (node.data + 1, high)))
            i += 1
    return root

def preOrder(root):
    if not root:
        return
    
    print(root.data, end=" ")
    preOrder(root.left)
    preOrder(root.right)

if __name__ == "__main__":
    arr = [7, 4, 12, 3, 6, 8, 1, 5, 10]
    root = constructBst(arr)
    preOrder(root)
C#
// C# program to Construct BST from 
// its given level order traversal
using System;
using System.Collections.Generic;

class Pair<X, Y> {
    public X first;
    public Y second;
    public Pair(X first, Y second) {
        this.first = first;
        this.second = second;
    }
}

class Node {
    public int data;
    public Node left;
    public Node right;
    public Node(int val) {
        data = val;
        left = null; 
        right = null;
    }
}

class GfG {
    static Node ConstructBst(int[] arr) {
        int n = arr.Length;
        
        Node root = new Node(arr[0]);
        Queue<Pair<Node, Pair<int, int>>> q = 
        new Queue<Pair<Node, Pair<int, int>>>();
        q.Enqueue(new Pair<Node, Pair<int, int>>(root, 
        new Pair<int, int>(int.MinValue, int.MaxValue)));
        
        int i = 1;
        while (i < n && q.Count > 0) {
            var curr = q.Dequeue();
            
            Node node = curr.first;
            int low = curr.second.first;
            int high = curr.second.second;
            
            // Check for left child
            if (i < n && arr[i] >= low && arr[i] < node.data) {
                node.left = new Node(arr[i]);
                q.Enqueue(new Pair<Node, Pair<int, int>>(node.left, 
                new Pair<int, int>(low, node.data - 1)));
                i++;
            }
            
            // Check for right child
            if (i < n && arr[i] > node.data && arr[i] <= high) {
                node.right = new Node(arr[i]);
                q.Enqueue(new Pair<Node, Pair<int, int>>(node.right,
                new Pair<int, int>(node.data + 1, high)));
                i++;
            }
        }
        return root;
    }

    static void PreOrder(Node root) {
        if (root == null) return;
        
        Console.Write(root.data + " ");
        PreOrder(root.left);
        PreOrder(root.right);
    }

    static void Main() {
        int[] arr = {7, 4, 12, 3, 6, 8, 1, 5, 10};
        Node root = ConstructBst(arr);
        PreOrder(root);
    }
}
JavaScript
// JavaScript program to Construct BST from 
// its given level order traversal

class Pair {
    constructor(first, second) {
        this.first = first;
        this.second = second;
    }
}

class Node {
    constructor(val) {
        this.data = val;
        this.left = null;
        this.right = null;
    }
}

function constructBst(arr) {
    let n = arr.length;
    
    let root = new Node(arr[0]);
    let q = [];
    q.push(new Pair(root, new Pair(-Infinity, Infinity)));
    
    let i = 1;
    while (i < n && q.length > 0) {
        let curr = q.shift();
        
        let node = curr.first;
        let low = curr.second.first;
        let high = curr.second.second;
        
        // Check for left child
        if (i < n && arr[i] >= low && arr[i] < node.data) {
            node.left = new Node(arr[i]);
            q.push(new Pair(node.left, new Pair(low, node.data - 1)));
            i++;
        }
        
        // Check for right child
        if (i < n && arr[i] > node.data && arr[i] <= high) {
            node.right = new Node(arr[i]);
            q.push(new Pair(node.right, new Pair(node.data + 1, high)));
            i++;
        }
    }
    return root;
}

function preOrder(root) {
    if (!root) return;
    
    process.stdout.write(root.data + " ");
    preOrder(root.left);
    preOrder(root.right);
}
// Driver Code
let arr = [7, 4, 12, 3, 6, 8, 1, 5, 10];
let root = constructBst(arr);
preOrder(root);

Output
7 4 3 1 6 5 12 8 10 
Comment