Construct Tree from given Inorder and Preorder Traversals in Java

Construct Tree from Given Inorder and Preorder Traversals

Constructing a binary tree from traversal data is a very common and important problem in Data Structures. When we are given Inorder and Preorder traversals, we can uniquely reconstruct the original binary tree.

In this article, we will understand the concept, approach, algorithm, Java implementation, examples, and best practices in a clean and beginner-friendly way.

construct a tree from preorder and inorder traversal

What are Inorder and Preorder Traversals?

1. Preorder (Root → Left → Right)

  • First element is always the root

2. Inorder (Left → Root → Right)

  • Elements left of root → left subtree
  • Elements right of root → right subtree

Given Inorder and Preorder Tree Traversals, construct the Tree

Example:

Given traversals:

Preorder: 50, 25, 12, 37, 30, 40, 75, 62, 60, 70, 87

Inorder: 12, 25, 30, 37, 40, 50, 60, 62, 70, 75, 87

  1. The last element in the preorder traversal is the root of the tree.
    So, here 50 will be the root of the tree.
  2. We will find the index of 50 in the inorder traversal.The index found is 5. Let this index is denoted by ‘pos’.
  3. All the elements to the left of this index ( from 0 to 4 index) will be in the left subtree of 50.
  4. And all the elements to the right of this index ( from 6 to 10) will be in the right subtree of 50.
what is the structure of tree

Algorithm for Given Inorder and Preorder Tree Traversals

Step by Step Algorithm:

  1. Pick the first element from preorder → root
  2. Find root index in inorder array
  3. Elements before index → left subtree
  4. Elements after index → right subtree
  5. Recursively repeat for left and right

Learn DSA

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Dividing Preorder and Inorder array in 2 parts:

Now, we will divide preorder and inorder array in two parts.
One is for the left subtree and other is for the right subtree.

Let suppose:

  • psi: starting index for preorder array
  • pei: ending index for preorder array
  • isi: starting index of inorder array
  • iei: ending index of preorder array
  • clc: Number of elements in the left subtree

Clearly, clc= pos – isi;

For left subtree:
Preorder array: from index psi + 1, psi + clc
Inorder array: from index isi, isi + clc -1

For right subtree:
Preorder array: from index psi + clc + 1, pei
Inorder array: from index isi + clc + 1, iei

Using the above arrays, all the steps are recursively repeated.

The following binary tree is obtained:

Construct Tree from given Inorder and Preorder traversals

Pseudocode to Construct Tree from given Traversals

buildTree(preorder, inorder):
    if no elements:
        return null

    root = preorder[preIndex]
    preIndex++

    find root index in inorder

    root.left = build left subtree
    root.right = build right subtree

    return root

Java Code to Construct Tree from given Traversals

Run
import java.util.*;

class Node {
    int data;
    Node left, right;

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

public class ConstructTree {

    static int preIndex = 0;

    public static Node buildTree(int[] preorder, int[] inorder, int start, int end) {

        if (start > end)
            return null;

        // Pick current node from preorder
        int rootValue = preorder[preIndex++];
        Node root = new Node(rootValue);

        // If leaf node
        if (start == end)
            return root;

        // Find root index in inorder
        int inIndex = search(inorder, start, end, rootValue);

        // Build left and right subtrees
        root.left = buildTree(preorder, inorder, start, inIndex - 1);
        root.right = buildTree(preorder, inorder, inIndex + 1, end);

        return root;
    }

    public static int search(int[] inorder, int start, int end, int value) {
        for (int i = start; i <= end; i++) {
            if (inorder[i] == value)
                return i;
        }
        return -1;
    }

    // Inorder traversal for verification
    public static void inorderPrint(Node root) {
        if (root == null) return;

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

    public static void main(String[] args) {

        int[] preorder = {1, 2, 4, 5, 3, 6};
        int[] inorder  = {4, 2, 5, 1, 3, 6};

        Node root = buildTree(preorder, inorder, 0, inorder.length - 1);

        System.out.print("Constructed Tree (Inorder): ");
        inorderPrint(root);
    }
}

Input :

Preorder → 1 2 4 5 3 6
Inorder → 4 2 5 1 3 6

Constructed Tree:

        1
       / \
      2   3
     / \   \
    4   5   6

Output :

Constructed Tree (Inorder): 4 2 5 1 3 6

Conclusion

Common Insights:

  1. Preorder decides root
  2. Inorder divides tree
  3. Combination uniquely defines tree
  4. Not possible with only one traversal

Best Practices:

  1. Use HashMap to optimize search
  2. Avoid duplicate values
  3. Validate input arrays
  4. Keep recursion clean and modular
  5. Always verify with inorder traversal

Frequently Asked Questions

Answer:

It is a method to rebuild a binary tree using preorder (root first) and inorder (left root right) traversal arrays.

Answer:

It selects root from preorder and splits the inorder array into left and right subtrees recursively.

Answer:

It is O(n²) in the basic approach and O(n) when optimized using a HashMap.

Answer:

No, we need at least two traversals (like preorder + inorder) to uniquely construct a tree.

Answer:

It is used in expression trees, serialization, parsing, and compiler design.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion : C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal Line by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric – C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree- CC++ | Java

Introduction to Trees

Binary Trees

Binary Search Trees

Traversals

  • Traversal in Trees
  • Tree Traversals: Breadth-First Search (BFS) : C | C++ | Java
  • Tree Traversals: Depth First Search (DFS) : C | C++ | Java
  • Construct a Binary Tree from Postorder and Inorder

B – Trees

AVL Trees

  • AVL Trees
    • AVL Trees: Introduction
    • AVL Tree Insertion :  C | C++ | Java
    • AVL Tree Deletion : C | C++ | Java
    • Insertion in a Binary Tree (Level Order) – C | C++ | Java
    • Searching in Binary Tree – C | C++ | Java
    • Searching in a Binary Search Tree – C | C++ | Java

Complete Programs for Trees

  • Depth First Traversals – C | C++ | Java
  • Level Order Traversal – C | C++Java
  • Construct Tree from given Inorder and Preorder traversals – C | C++Java
  • Construct Tree from given Postorder and Inorder traversals – C | C++Java
  • Construct Tree from given Postorder and Preorder traversal – C | C++Java
  • Find size of the Binary tree – C | C++Java
  • Find the height of binary tree – C | C++Java
  • Find maximum in binary tree – C | C++Java
  • Check whether two tree are identical- CC++Java
  • Spiral Order traversal of Tree- CC++Java
  • Level Order Traversal LIne by Line – C | C++Java
  • Hand shaking lemma and some Impotant Tree Properties.
  • Check If binary tree if Foldable or not.- CC++Java
  • check whether tree is Symmetric  C| C++Java.
  • Check for Children-Sum in Binary Tree- C|C++Java
  • Sum of all nodes in Binary Tree- CC++ | Java
  • Lowest Common Ancestor in Binary Tree. CC++ | Java