Construct Tree from given Postorder and Inorder Traversals in Java
Construct Tree From Inorder and Postorder Traversals
Here we will practice Java Code to Construct Tree From Inorder and Postorder Traversals with proper example and algorithm explanation.
At last we have mentioned the Time and space complexity analysis of this topic with some common insights and best ways to practice this concept.
What are Inorder and Postorder Traversals?
1. Inorder (Left → Root → Right)
- Left subtree nodes come first
- Then root
- Then right subtree
2. Postorder (Left → Right → Root)
- Last element is always the root
Construct Tree From Inorder and Postorder Traversals
Example:
Given traversals:
- Postorder: 12, 30, 40, 37, 25, 60, 70, 62, 87, 75, 50
- Inorder: 12, 25, 30, 37, 40, 50, 60, 62, 70, 75, 87
The last element in the postorder 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 to Construct tree from Inorder and Postorder Traversals
Step by Step Algorithm:
- Pick last element from postorder → root
- Find root index in inorder array
- Elements left → left subtree
- Elements right → right subtree
- Recursively:
- Build right subtree
- Build left subtree
- Return root
Learn DSA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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:
Postorder array: from index psi, psi + clc – 1
Inorder array: from index isi, isi + clc -1
For right subtree:
Postorder array: from index psi+clc, pei – 1
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(postorder, inorder):
if no elements:
return null
root = postorder[postIndex]
postIndex--
find root index in inorder
root.right = build right subtree
root.left = build left 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 ConstructTreePostIn {
static int postIndex;
public static Node buildTree(int[] inorder, int[] postorder, int start, int end) {
if (start > end)
return null;
// Pick root from postorder
int rootValue = postorder[postIndex--];
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 right subtree first
root.right = buildTree(inorder, postorder, inIndex + 1, end);
root.left = buildTree(inorder, postorder, start, inIndex - 1);
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[] inorder = {4, 2, 5, 1, 3, 6};
int[] postorder = {4, 5, 2, 6, 3, 1};
postIndex = postorder.length - 1;
Node root = buildTree(inorder, postorder, 0, inorder.length - 1);
System.out.print("Constructed Tree (Inorder): ");
inorderPrint(root);
}
}
Input :
Inorder → 4 2 5 1 3 6 Postorder → 4 5 2 6 3 1
Constructed Tree:
1
/ \
2 3
/ \ \
4 5 6
Output :
Constructed Tree (Inorder): 4 2 5 1 3 6
2. Split inorder around 1
3. Build right subtree first (important)
4. Then build left subtree
5. Recursively repeat
Optimized: O(n) using HashMap
Worst case: O(n), Best case: O(log n)
Conclusion….
Common Insights:
- Postorder gives root at the end
- Inorder divides tree
- Right subtree must be built first
- Combination uniquely defines tree
Best Practices:
- Use HashMap for faster lookup
- Always build right subtree first
- Avoid duplicate values
- Validate traversal arrays
- Test edge cases
Frequently Asked Questions
Answer:
It is a method to rebuild a binary tree using inorder (left-root-right) and postorder (left-right-root) traversal arrays.
Answer:
Because we process postorder from the end, which gives root and then right subtree elements.
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 inorder + postorder) to uniquely construct a tree.
Answer:
It is used in expression trees, parsing, compilers, and serialization systems.
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
