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.
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
- The last element in the preorder traversal is the root of the tree.
So, here 50 will be the root of the tree. - We will find the index of 50 in the inorder traversal.The index found is 5. Let this index is denoted by ‘pos’.
- All the elements to the left of this index ( from 0 to 4 index) will be in the left subtree of 50.
- And all the elements to the right of this index ( from 6 to 10) will be in the right subtree of 50.
Algorithm for Given Inorder and Preorder Tree Traversals
Step by Step Algorithm:
- Pick the first element from preorder → root
- Find root index in inorder array
- Elements before index → left subtree
- Elements after index → right subtree
- 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:
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
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
2. In inorder, elements left of 1 → left subtree
3. Elements right of 1 → right subtree
4. Recursively repeat for subtrees
Optimized: O(n) using HashMap
Worst case: O(n), Best case: O(log n)
Conclusion
Common Insights:
- Preorder decides root
- Inorder divides tree
- Combination uniquely defines tree
- Not possible with only one traversal
Best Practices:
- Use HashMap to optimize search
- Avoid duplicate values
- Validate input arrays
- Keep recursion clean and modular
- 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
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree- C | C++ | Java
Introduction to Trees
Binary Trees
- Binary Tree in Data Structures (Introduction)
- Tree Traversals: Inorder Postorder Preorder : C | C++ | Java
- Inorder Postorder PreOrder Traversals Examples
- Tree Traversal without Recursion
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
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- C| C++| Java
- Spiral Order traversal of Tree- C | C++| 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.- C| C++| 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- C | C++ | Java
- Lowest Common Ancestor in Binary Tree. C | C++ | Java
